Sto provando a scrivere del testo su un file usando il metodo Files.write()
.Java Files.write NoSuchFileException
byte[] contents = project.getCode().getBytes(StandardCharsets.UTF_8);
try {
Files.write(project.getFilePath(), contents, StandardOpenOption.CREATE);
} catch (IOException ex) {
ex.printStackTrace();
return;
}
Secondo l'API, se il file non esiste, verrà creato e poi scritti.
Tuttavia, ottengo questo:
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)
mi sto perdendo qualcosa?
La directory 'C: \ Users \ Administrator \ Desktop \ work' esiste? (e perché sviluppi come amministratore?) – fge
La cartella è mancante? –
usa file.getParentFile(). Mkdirs(); –