在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!