How to create directories in Java

Java directory FAQ: How do I create a directory (or directories) in Java? (Also written as, "How do I make a directory in Java?)

To create a directory in Java, just use the "mkdir" or "mkdirs" methods of the Java File class. Here are a few examples to demonstrate this.

Java - Create directories with mkdir

Here's an example program that demonstrates how to create a directory with the Java mkdir method.

import java.io.File;

// demonstrates how to create a directory in java
public class JavaCreateDirectoryExample
{
  public static void main(String[] args)
  {
    File dir = new File("/Users/al/tmp/TestDirectory");
    
    // attempt to create the directory here
    boolean successful = dir.mkdir();
    if (successful)
    {
      // creating the directory succeeded
      System.out.println("directory was created successfully");
    }
    else
    {
      // creating the directory failed
      System.out.println("failed trying to create the directory");
    }
  }
}

The first time you run this program the directory will be created successfully, and if you try to run it multiple times, it will fail on each successive attempt, because the directory already exists.

Create several layers of directories in Java with mkdirs

If you need to create multiple directories at one time, you can use the "mkdirs" method of the Java File class, as shown in this example:

import java.io.File;

// shows how to create multiple directories in java
// (multiple directory levels)
public class JavaCreateMultipleDirectoriesExample
{
  public static void main(String[] args)
  {
    // the folders "000/111/222" don't exist initially
    File dir = new File("/Users/al/tmp/000/111/222");
    
    // create multiple directories at one time
    boolean successful = dir.mkdirs();
    if (successful)
    {
      // created the directories successfully
      System.out.println("directories were created successfully");
    }
    else
    {
      // something failed trying to create the directories
      System.out.println("failed trying to create the directories");
    }
  }
}

Like the "mkdir" program, the first time you run this "mkdirs" program the directories should be created successfully, and if you try to run it multiple times, it will fail on each successive attempt, because the directories already exists.

Here's a blurb from the Java File javadoc for the mkdirs method, explaining how it works:

"Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories."

Java File mkdir and mkdirs methods

As you can see, both methods (mkdir, and mkdirs) return a boolean value indicating whether the directories were created successfully. You'll want to include your own logic in the if/then tests that are shown after the mkdir and mkdirs method calls.

Finally, as you can see, these "create directory" methods will return "false" if the directories already exist before you try to create them. Therefore, I strongly recommend using the "exists" method of the Java File class before running mkdir/mkdirs. That way you can accurately know if creating the directories truly failed.

Post new comment

The content of this field is kept private and will not be shown publicly.