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 :
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!");
}
}
}
If both path name are same :
| Both paths are same! |
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.
Ask Questions? Discuss: File Path compare in java
Post your Comment