So sánh File path trong Java



Miêu tả vấn đề

Cách so sánh path của hai file trong Java?

Giải pháp

Ví dụ sau minh họa cách so sánh path của hai file trong cùng một thư mục bởi sử dụng phương thức filename.compareTo (another filename) của lớp File trong Java.

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!");
      }
   }
}

Kết quả

Code trên sẽ cho kết quả sau:

Paths are not same!

file_trong_java.jsp