Java NIO Files.createFile() 失败并出现 NoSuchFileException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27335005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:38:28  来源:igfitidea点击:

Java NIO Files.createFile() fails with NoSuchFileException

javafilenio

提问by casgage

I am trying to lay down some core files in a dev-test-prod setup. Basically if the file is newer it needs to be copied to the next level as part of the QA process.

我正在尝试在 dev-test-prod 设置中放置一些核心文件。基本上,如果文件较新,则需要将其复制到下一个级别,作为 QA 流程的一部分。

I am using Java 8 so I decided to try the NIO Files/Path apis for the first time. I am creaky old, having been programming for 48 years and have used almost exclusively Java since early 1996, and every release since prerelease, so this NIO "enhancement" should not be too hard for me to assimilate, but . . .

我使用的是 Java 8,所以我决定第一次尝试 NIO Files/Path api。我老了,已经编程 48 年了,从 1996 年初开始几乎只使用 Java,并且自预发布以来的每个版本都使用 Java,所以这个 NIO 的“增强”对我来说应该不会太难吸收,但是 . . .

FileSystem fs = FileSystems.getDefault();
Path in = fs.getPath(fromFileName);
Path out = fs.getPath(toFileName);

if (Files.exists(out)) {
  FileTime inTime = Files.getLastModifiedTime(in);
  FileTime outTime = Files.getLastModifiedTime(out);

  if (0 > outTime.compareTo(inTime)) {
    Files.copy(in, out, StandardCopyOption.REPLACE_EXISTING);
  }
} else {
  Files.createFile(out);
  Files.copy(in, out);
}

I initially just tried Files.copy() without Files.createFile() and got a NoSuchFileExceptionon the copy() call.

我最初只是在没有 Files.createFile() 的情况下尝试 Files.copy() 并在 copy() 调用中得到NoSuchFileException

I looked a several StackOverflow posts which referred to this, one of which stated authoritatively that copy() will fail if the destination file does not already exist. For the life of me I cannot understand why the designers thought this was a good idea, but so be it. I accordingly added the createFile() call as above (having read the API doc for Files which says that Files.createFile() "Creates a new and empty file, failing if the file already exists." When I ran it again I got exactly the same Exception, but on the createFile() instead of the copy(). Notice the path is inside my home directory on Windows, so no access denied issues should occur. Also NOTHING other than Eclipse containing this project is running on my PC at this time.

我查看了几篇 StackOverflow 帖子,其中提到了这一点,其中一篇权威地声明,如果目标文件不存在,copy() 将失败。对于我的生活,我无法理解为什么设计师认为这是一个好主意,但就这样吧。我相应地添加了上面的 createFile() 调用(已阅读文件的 API 文档,其中说 Files.createFile() “创建一个新的空文件,如果文件已经存在则失败。”当我再次运行它时,我得到了相同的异常,但在 createFile() 而不是 copy() 上。注意路径在我在 Windows 上的主目录中,所以不会发生访问被拒绝的问题。除了包含这个项目的 Eclipse 之外,我的 PC 上没有其他任何东西在运行这次。

java.nio.file.NoSuchFileException: C:\Users\ChrisGage\myproject\site\ttws\css\core.css
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown Source)
at java.nio.file.Files.newByteChannel(Unknown Source)
at java.nio.file.Files.createFile(Unknown Source)
...

What am I doing wrong?

我究竟做错了什么?

回答by fge

Files.copy()(and Files.move()for that matter) is "dumb"; it won't try and do any of the following:

Files.copy()Files.move()就此而言)是“愚蠢的”;它不会尝试执行以下任何操作:

  • copy entire directory hierarchies;
  • move entire directory hierarchies (if the source and target are on different filesystems);
  • create missing directories etc.
  • 复制整个目录层次结构;
  • 移动整个目录层次结构(如果源和目标在不同的文件系统上);
  • 创建丢失的目录等。

You need to do:

你需要做:

final Path tmp = out.getParent();
if (tmp != null) // null will be returned if the path has no parent
    Files.createDirectories(tmp);

prior to copying the file.

在复制文件之前。