Java 8 comes with very easy ways to Java create directory or  Java create directory if not exist. Here are different ways to Java create directory or Java create directory if not exist.

1. Java Create directory

Files.createDirectory will create new directory but  it is required to create all nonexistent parent directories first. It will not create parent directory automatically.

try {
            Path path = Files.createDirectory(Paths.get("F:\\newJavaDir\\TempDir"));     // it will not create parent directory, only create last child directory
            System.out.println(path.toString());
        }catch (IOException e){
            e.printStackTrace();
        }

2. Java Create Directory if not exist (Using NIO)

Files.createDirectories will create directory including parent directory. If parent directory is not exist than create all parent directory and than create child directory. It will create directory if not exist. It will return Path object.

try {
           Path path = Files.createDirectories(Paths.get("F:\\newJavaDir\\TempDir"));      // it will create directory recursive if not exist
           System.out.println(path.toString());
       }catch (IOException e){
           e.printStackTrace();
       }

3. Java Create Directory if not exist (Using mkdirs)

File.mkdirs is also method using that we can create method

File file=new File("F:\\newJavaDir1\\TempDir");
boolean isSuccess = file.mkdirs();    // it will create directory recursive and return boolean, true for success, false for failed

References:

Java 8 Files Documentation

Java File Documentation 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *