Using Java nio to create a subdirectory and file
You could declare your confFile
as File
instead of Path
. Then you can use confFile.getParentFile().mkdirs();
, see example below:
// ...
File confFile = new File("./conf/conf.xml");
confFile.getParentFile().mkdirs();
// ...
Or, using your code as is, you can use:
Files.createDirectories(confFile.getParent());
You can create directory and file in one code line:
Files.createFile(Files.createDirectories(confDir).resolve(confFile.getFileName()))
Files.createDirectories(confDir)
will not throw an exception if the folder already exists and returns Path in any case.