How to compare paths of two files ?


Levels of difficulty: / perform operation:

Problem & Solution

This example shows how to compare paths of two files in a same directory by the use of filename.compareTo (another filename) method of File class.

JAVA Program

import java.io.File;

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

Output

The above code sample will produce the following result.

Paths are not same!