Skip to content
Advertisement

Using Java nio to create a subdirectory and file

I’m creating a simple program that will try to read in “conf/conf.xml” from disk, but if this file or dir doesn’t exist will instead create them.

I can do this using the following code:

JavaScript

My questions is if this really the most elegant way to do this? It seems superflous to need to create two Paths simple to create a new file in a new subdirectory.

Advertisement

Answer

You could declare your confFile as File instead of Path. Then you can use confFile.getParentFile().mkdirs();, see example below:

JavaScript

Or, using your code as is, you can use:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement