File Path compare in java

Learn how to compare file path in Java?

File Path compare in java

Learn how to compare file path in Java?

File Path compare 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