【问题标题】:Is there a reason to get the file name of a path before converting it to a file?是否有理由在将路径转换为文件之前获取路径的文件名?
【发布时间】:2017-12-22 02:21:23
【问题描述】:

有人回答one of my questions 使用path.getFileName().toFile() 而不仅仅是path.toFile()。这是有原因的还是我应该只使用path.toFile()

【问题讨论】:

  • 为什么不问问回答你问题的人?
  • 对不起。下次我会照你说的做。

标签: java path nio


【解决方案1】:

在您的具体情况下,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

【讨论】:

    猜你喜欢
    • 2021-10-18
    • 2011-11-16
    • 2012-09-30
    • 2012-12-19
    • 2016-12-11
    • 1970-01-01
    • 2015-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多