在java程式設計中,如何比較兩個檔案的路徑?
此範例顯示如何使用File
類的filename.compareTo(String filename)
方法來比較同一目錄中兩個檔案的路徑。
package com.yiibai;
import java.io.File;
public class FilePathComparison {
public static void main(String[] args) {
File file1 = new File("F:/worksp/javaexamples/java_files/demo1.txt");
File file2 = new File("F:/worksp/javaexamples/java_date_time/demo1.txt");
if (file1.compareTo(file2) == 0) {
System.out.println("Both paths are same!");
} else {
System.out.println("Paths are not same!");
}
}
}
執行上面範例程式碼,得到以下結果 -
Paths are not same!