Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Determining if two Filename paths refer to the same file.

                         

This program is going to determine that the file name refers to the same file or not. What you need to do is to create two File object and pass the file name in the constructor of both the File class. Now match both the file instances. It will return false if the file paths are not equal. To send the true value we first have to normalize both the paths, only after that  it will return true. 

In this program we have used following methods: 

equals():
This is the method of String class. It checks whether the pathname are equal or not. 

getCanonicalFile():
It denotes the same file or directory its abstract pathname. It throws IOException and SecurityException.

Code of the program is given below:

import java.io.*;

  public class FileNameReferToSameFile{
    public static void main(String args[]){
      File file1 = new File("./filename.txt");
        File file2 = new File("filename.txt");
      System.out.println("Actual fileName1 = " + file1 + "\n");
          System.out.println("Actual fileName2 = " + file2 + "\n");
    
      // It returns false if Filename paths are not equal 
        boolean b = file1.equals(file2);      
      System.out.println("It checks whether the file name 

paths are equal or not"  +  b);

        

// Normalize the paths
        try {
            file1 = file1.getCanonicalFile(); // c:\tapan\filename
        file2 = file2.getCanonicalFile();  // c:\tapan\filename
        System.out.println("Actual path of filName1 = " + file1 + "\n");
            System.out.println("Actual path of fileName2 = " + file2 + "\n");
        catch (IOException e) {
 System.out.println("IOException is "+ e);
 }
    
        // It returns true if Filename paths are  equal
        b = file1.equals(file2);              
      System.out.println("the file name are now equal"  +   b);
      System.out.println("Actual path of fileName1 = " + file1 + "\n");
          System.out.println("Actual path of fileName2 = " + file2 + "\n");
 }
  }

Output of this program is given below:

C:\FileNameReferToSameFile>java FileNameReferToSameFile
Actual Filename1 = .\FileNameReferToSameFile.txt

Actual Filename2 = FileNameReferToSameFile.txt

It checks whether the file name are equal or notfalse
Actual Filename1 = C:\FileNameReferToSameFile\FileNameReferToSameFile.txt

Actual Filename2 = C:\FileNameReferToSameFile\FileNameReferToSameFile.txt

the file name are now equaltrue
Actual Filename1 = C:\FileNameReferToSameFile\FileNameReferToSameFile.txt

Actual Filename2 = C:\FileNameReferToSameFile\FileNameReferToSameFile.txt

Download this example:

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

Good piece of code
Thank You

Kamal Chauhan

Gurgaon

Posted by Kamal Chauhan on Saturday, 12.23.06 @ 18:00pm | #1506

These tutorials are good and practical. I appreciate ur work..

Thanx

Russel Fyodor
Moscow

Posted by Fyodor Russel on Saturday, 12.23.06 @ 17:58pm | #1504

Latest Searches:
Photoshop Effects Shin
string array add eleme
set cell value JTable
Java Tuthreadstorials
view record by charact
Sams Teach Yourself Mi
basic flow of struts
dojo tree
excel from java
How to handle JSP Erro
arraylist storing mult
dwr reverse ajax
midlet to create anim
Javascript Text Effect
Janino -- an Embedded
grid
editor in JSF
about java Bean
java arrays
online bookstore in ja
compare two dates in j
how to upload image ur
how to capture multipl
string operations
Ajax Login Example
j2me servlet
Cinema 4D Model a Glas
sequential
onetomany example in h
how to create forum
lines of text in rever
EL Implicit Objects
calling one jsp p
how to concate two str
image upload
Convert Integer to Dou
value change listener
JBI
Javascript Mouse Trick
prime numbers
JDBC with Mysql in Ecl
abstrct path name of f
netui:select
military time
ASP.NET .NET Add text
Why Virtual Dedicated
advantages of jsf
jsp bean get property
uploaded file
FCKediter
Flex TreeGrid
java date compare exam
Photoshop Text Effects
POI Excel
Get Array List in a JS
Photoshop Drawing Orga
jQuery to Retrieve Ser
struts2 action element
pthread.h
Merge Sort In Java
word count in a file
Tree Grid
JSTL : Submit Form Tex
<x:WorksheetSource HRe
ajax
<h:selectOneListbox
what is local class?
Photoshop Effects Hole
how to pass same varia
JSP:File Input Paramet
Resource bundle
how to run a java
sample examples in spr
array in javascript
storing an XML file in
showMessage
Java Pass Value
array element in php
java Lang package math
swapping arrays
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.