【发布时间】:2017-12-22 02:21:23
【问题描述】:
有人回答one of my questions 使用path.getFileName().toFile() 而不仅仅是path.toFile()。这是有原因的还是我应该只使用path.toFile()?
【问题讨论】:
-
为什么不问问回答你问题的人?
-
对不起。下次我会照你说的做。
有人回答one of my questions 使用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
foo\bar\buzz
【讨论】: