比较Java中的两个文件路径
可以使用java.io.File.compareTo()方法在Java中按字典顺序比较两个文件路径。此方法需要一个参数,即要比较的抽象路径名。如果两个文件路径名相等,则返回0。
演示此的程序如下所示-
示例
import java.io.File;
public class Demo {
public static void main(String[] args) {
File file1 = new File("C:/File/demo1.txt");
File file2 = new File("C:/File/demo1.txt");
if (file1.compareTo(file2) == 0) {
System.out.println("Both the paths are lexicographically equal");
} else {
System.out.println("Both the paths are lexicographically not equal");
}
}
}上面程序的输出如下-
输出结果
Both the paths are lexicographically equal
现在让我们了解上面的程序。
方法java.io.File.compareTo()用于按字典顺序比较两个文件路径。如果该方法返回0,则文件路径在字典上相等,否则不相等。证明这一点的代码片段如下-
File file1 = new File("C:/File/demo1.txt");
File file2 = new File("C:/File/demo1.txt");
if (file1.compareTo(file2) == 0) {
System.out.println("Both the paths are lexicographically equal");
} else {
System.out.println("Both the paths are lexicographically not equal");
}热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语