nio.channels.FileChannel.open抛出了NoSuchFileException。

huangapple go评论109阅读模式
英文:

nio.channels.FileChannel.open threw NoSuchFileException

问题

我有一个微服务,大多数情况下都运行正常。最近在打开文件进行写入时抛出了一个 NoSuchFileException 异常:

FileChannel.open(Paths.get("/tmp/somethingirrelevant"), StandardOpenOption.CREATE, StandardOpenOption.APPEND);

我不明白为什么会抛出这样的异常,考虑到如果文件不存在,它会创建一个新文件。

英文:

I have a microservice that is working fine most of the times. Recently it threw a NoSuchFileException exception when it was opening a file to write:

    FileChannel.open(Paths.get("/tmp/somethingirrelevant"), StandardOpenOption.CREATE, StandardOpenOption.APPEND);

I don't understand why it can throw such exception, considering that it will create a new one if it doesn't exist.

答案1

得分: 2

在抛出 NoSuchFileException 的一种情况是当中间路径组件不存在:

FileChannel.open(Paths.get("/tmp/does/not/exist"), StandardOpenOption.CREATE, StandardOpenOption.APPEND);

CREATE 选项只会创建文件,而不会创建应该包含文件的目录。

英文:

One scenario where NoSuchFileException is thrown is when an intermediate path component does not exist:

FileChannel.open(Paths.get("/tmp/does/not/exist"), StandardOpenOption.CREATE, StandardOpenOption.APPEND);

The CREATE option only creates the file, it does not create the directories that should contain the file.

huangapple
  • 本文由 发表于 2020年8月29日 01:14:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63638250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定