【发布时间】:2021-02-25 13:41:14
【问题描述】:
try {
System.out.println(Files.isSameFile(Paths.get("/user/home/cobra"),
Paths.get("/user/home/snake"))); //line 1 outputs true
System.out.println(Files.isSameFile(Paths.get("/user/tree/../monkey"),
Paths.get("/user/monkey"))); //line 2 outputs true
System.out.println(Files.isSameFile(Paths.get("/leaves/./giraffe.exe"),
Paths.get("/leaves/giraffe.exe"))); // line 3 outputs true
} catch (IOException e) {
// Handle file I/O exception...
}
在上面的代码中,我预计第 1 行、第 2 行和第 3 行的输出为 false,因为路径不同,但对于所有这些实例都是如此,有人可以解释这是为什么吗?
【问题讨论】:
-
在 unix 系统上(在 Windows 上也是如此,但不是那么容易),您可以创建符号链接。这意味着路径 X 处的文件与其他路径中的文件相同
-
无法重现第一个。