是否有理由在将路径转换为文件之前获取路径的文件名?



有人用path.getFileName().toFile()回答了我的一个问题,而不仅仅是path.toFile()。有原因还是我应该只使用path.toFile()

在您的特定情况下,在这种情况下path.getFileName().toFile().getName()path.toFile().getName()会给您相同的结果。

但一般来说,path.getFileName().toFile()path.toFile()会返回不同的文件。

这里有个小例子。

Path path =  FileSystems.getDefault().getPath("foo", "bar", "buzz");
System.out.println(path.getFileName().toFile());
System.out.println(path.toFile());

这给了我们

buzz
foobarbuzz

最新更新