Creating Directory in Java

To create directories in Java, you will need to use the java.io package.

In the java.io package you will need to use the File class and use the two methods mkdir() and mkdirs().
Both of these methods will return a boolean indicating whether or not they were successful.

1. Creating a directory with mkdir()

Below you will see how I create a directory in java using mkdir() method.

package com.omega.tutorial;

import java.io.File;
import java.io.IOException;

public class CreateDirectoryExample {

  public static void main(String[] args) throws IOException {
    // create a new directory named dir1 in /tmp folder
    File directory = new File("C:/tmp/dir1");
    
    // try to create directory
    boolean isCreated = directory.mkdir();

    if (isCreated) {
      // successful in the directory creation
      System.out.println("Successfully created directory ===> " + directory.getCanonicalFile());
    } else {
      // unable to create directory
      System.out.println("Unable to create directory ===> " + directory.getCanonicalFile());
    }
  }
}

When we run this program for the very first time and the directory is not present, it will go ahead and create the directory for us, however, running the same program a second time will output the error message of “Unable to create directory” as this directory now exists.

Output:

create_directory

2. Creating nested directories in Java

Using the mkdirs() method allows us to create a directory and any nonexistent parent directories. What an easy may to create a nested directory tree in one command.

package com.omega.tutorial;

import java.io.File;
import java.io.IOException;

public class CreateDirectoryMultipleExample {

  public static void main(String[] args) throws IOException {
    // create a new directory named dir2 in /tmp folder
    File directory = new File("C:/tmp/dir2/sublevel1/sublevel2/sublevel3/sublevel4");
    
    // try to create directory
    boolean isCreated = directory.mkdirs();
  
    if (isCreated) {
      // successful in the directory creation
      System.out.println("Successfully created directories ===> " + directory.getCanonicalFile());
    } else {
      // unable to create directory
      System.out.println("Unable to create directories ===> " + directory.getCanonicalFile());
    }
  }
}

Output:

create_directory_tree

create_multiple_directory

Other Related Posts

  • How to Get Disk Space in Java
    Getting file statistics (ie. disk space) can now be easily done in Java 1.6 using new methods that we recently released.
  • Creating Directory in Java
    Learn how to create directories using these easy to follow instructions.
  • Delete a directory in Java
    Learn how to delete a directory using these easy to follow instructions. In the java.io package you will need to use the File class and use the delete() method.

Please Share Us on Social Media

Facebooktwitterredditpinterestlinkedinmail

Leave a Reply

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