Generate the required directories while saving files with Files.writeString

294 views Asked by At

I am saving a string as a file.

import java.nio.file.Files;
import java.nio.file.Path;

String data = "hello, world";
Files.writeString(Path.of("upload-dir", "xyz", "abc", "data.txt"), data);

I get the following error as xyz and abc are not present.

java.nio.file.NoSuchFileException: upload-dir/xyz/abc/data.txt

Is there a way to get this method to create those directories as well automatically (not by calling Files.createDirectories(filePath.getParent());?

0

There are 0 answers