Java Files.write NoSuchFileException

14 浏览
0 Comments

Java Files.write NoSuchFileException

我正在尝试使用Files.write()方法将一些文本写入文件。

byte[] contents = project.getCode().getBytes(StandardCharsets.UTF_8);
try {
    Files.write(project.getFilePath(), contents, StandardOpenOption.CREATE);
} catch (IOException ex) {
    ex.printStackTrace();
    return;
}

根据API的说明,如果文件不存在,它将被创建并写入。

然而,我遇到了这个问题:

java.nio.file.NoSuchFileException: C:\Users\Administrator\Desktop\work\Default.txt
    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.spi.FileSystemProvider.newOutputStream(Unknown Source)
    at java.nio.file.Files.newOutputStream(Unknown Source)
    at java.nio.file.Files.write(Unknown Source)

我有什么遗漏吗?

0