考虑以下例子:
如果我使用
//both files are the same
final File path = new File("/home/alice/../bob/file.txt");
final File canonicalPath = new File("/home/bob/file.txt");
File parent = canonicalPath;
while((parent = parent.getParentFile()) != null) {
System.out.println(parent.getName());
}
这将打印:
bob
home
如果我使用
path
而不是 canonicalPath
,输出结果会相同吗?还是会有所不同?bob
home
alice
home
这将会非常奇怪,因为它会暗示alice
是home
的父级,而这是不正确的。
final
parent
,循环也是无限的。 - Sotirios DelimanolisFile
而不是新的java.nio.file API? - fgepath
的输出。 - Sotirios Delimanolis