Using Java nio to create a subdirectory and file

36,926

Solution 1

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());

Solution 2

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.

Share:
36,926
user3341332
Author by

user3341332

Updated on May 25, 2020

Comments

  • user3341332
    user3341332 almost 4 years

    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:

        // create subdirectory path
        Path confDir = Paths.get("./conf"); 
    
        // create file-in-subdirectory path
        Path confFile = Paths.get("./conf/conf.xml"); 
    
        // if the sub-directory doesn't exist then create it
        if (Files.notExists(confDir)) { 
            try { Files.createDirectory(confDir); }
            catch (Exception e ) { e.printStackTrace(); }
        }
    
        // if the file doesn't exist then create it
        if (Files.notExists(confFile)) {
            try { Files.createFile(confFile); }
            catch (Exception e ) { e.printStackTrace(); }
        }
    

    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.

  • user3341332
    user3341332 about 10 years
    Wouldn't using mkdirs() or createDirectories(..) trip an exception if .conf/ already existed?I can see how it could be done with File, but wondered why the Paths/Files under nio didn't have an equally simply way to do this.
  • user207421
    user207421 about 10 years
    @user3341332 No. It doesn't throw exceptions at all. See the Javadoc.
  • PhoneixS
    PhoneixS about 9 years
    But note that Path.getParent() return null if there is no more parts in the "string" of the Path. For example a Path with dir/b.txt will return path dir but dir.getParent() will return null. So Files.createDirectories(dir.getParent()); throw a NullPointerException although diris inside other directory.
  • Adam
    Adam over 6 years
    why go back to java.io.File when you're starting with java.nio.Path?
  • Adam
    Adam over 6 years
  • mks-d
    mks-d over 3 years
    Files.createDirectories(confDir) will throw a FileAlreadyExistsException, if the directory already exists.
  • Valeriy K.
    Valeriy K. over 3 years
    @mks-d I verified - run the code 2 times and don't get the exception. Which java version you use?
  • mks-d
    mks-d about 3 years
    @ValeriyK. good point. And my bad since it was with createDirectory​(Path) and not createDirectories(Path). The version was ancient, Java 8.