Home Java Example Java Io File Path compare in java



File Path compare in java
Posted on: February 19, 2011 at 12:00 AM
Learn how to compare file path in Java?

File Path Comparison :compareTo

    

File Path Comparison :compareTo

In this section, we will discuss how to compare pathname of two file for the equality of their path.  For this purpose, we use   compareTo() method.

First ,we create two instance of class-same or different and check there equality using compareTo() method. The compareTo() method return 0(zero), if the argument is equal to this abstract pathname.

Given below example will give you a clear idea :

Example :

import java.io.File;

public class FilePathCompare {
public static void main(String[] args) {
File file1 = new File("C:/Folder/dev.txt");
File file2 = new File("C:/Folder/dev.txt");
if (file1.compareTo(file2) == 0) {
System.out.println("Both paths are same!");
} else {
System.out.println("Paths are not same!");
}
}
}

Output :

If both path name are same :

Both paths are same!   

Download Source Code

Related Tags for File Path compare in java:


More Tutorials from this section

Ask Questions?    Discuss: File Path compare in java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.