Share on Google+Share on Google+

File Path compare in java

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 :


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



Posted on: February 19, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: File Path compare in java  

Post your Comment

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