英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论