How to compare paths of two files in java

/* How to compare paths of two files? */
This example shows how to compare paths of two files or two file paths using compareTo() function
import java.io.File;
public class Main {
public static void main(String[] args)
{
// Create first file object
File file1 = new File(“C:/File/demo1.txt”);
// Create second file object
File file2 = new File(“C:/java/demo1.txt”);
//Compare two paths
if(file1.compareTo(file2) == 0)
{
System.out.println(“Both paths are same!”);
}

else
{
System.out.println(“Paths are not same!”);
}
}
}
Output:-
Paths are not same!

Leave a Reply