HOWTO · Java

How to Create Directory in Java

This tutorial discusses how to create a directory in Java

On this page

A directory is a file-system cataloging structure for storing files, and creating one is a common file-handling task in Java. The JDK offers two APIs for it — the legacy java.io.File methods and the newer java.nio.file.Files helpers — and Apache Commons IO adds a third option. This tutorial shows each approach and the behavior differences that matter when choosing one.

Choose a Java directory-creation method

For most new code, prefer java.nio.file.Files. Call Files.createDirectories(path) when some parent directories may not exist yet: it creates the whole path in one call and does not fail just because the directory is already there. Call Files.createDirectory(path) instead when an existing target should be treated as a failure. The older File.mkdir() and File.mkdirs() methods report success only through a boolean, which is easy to ignore, while the Files methods throw checked exceptions that name the exact problem. FileUtils.forceMkdir() is a reasonable choice only in codebases that already depend on Apache Commons IO.

The important differences are how each method handles missing parent directories and an already-existing target:

Method Missing parents Existing target directory Failure signal
File.mkdir() fails, returns false returns false boolean return value
File.mkdirs() creates them returns false boolean return value
Files.createDirectory() NoSuchFileException FileAlreadyExistsException checked IOException subtypes
Files.createDirectories() creates them returns normally checked IOException subtypes
FileUtils.forceMkdir() creates them returns normally IOException

Create one directory with File.mkdir()

File.mkdir() creates exactly one directory at the path held by the File instance. It returns true only when it actually created the directory; it returns false when the name is already taken, when the parent directory does not exist, or when the operating system denies the request, so always test the return value instead of assuming success.

The following program creates one directory named reports in the working directory and prints the result:

import java.io.File;

public class MkdirExample {
    public static void main(String[] args) {
        File dir = new File("reports");
        boolean created = dir.mkdir();
        System.out.println("mkdir() returned: " + created);
        System.out.println("Directory exists: " + dir.isDirectory());
    }
}
mkdir() returned: true
Directory exists: true

A false result tells you nothing about the cause — check dir.isDirectory() afterward when a pre-existing directory is acceptable, or switch to Files.createDirectory() for typed failures. mkdir() can also throw the unchecked SecurityException when a security manager denies write access.

Create a directory tree with File.mkdirs()

File.mkdirs() creates the target directory and every missing parent along the path in a single call:

import java.io.File;

public class MkdirsExample {
    public static void main(String[] args) {
        File dir = new File("reports/2026/october");
        boolean created = dir.mkdirs();
        System.out.println("mkdirs() returned: " + created);
        System.out.println("Directory exists: " + dir.isDirectory());
    }
}
mkdirs() returned: true
Directory exists: true

Note that mkdirs() returns true only when it created the directory together with all necessary parents. Running it on an existing tree returns false even though nothing went wrong, and a false result does not say which level failed. Verify with isDirectory() when success simply means the directory is there.

Create one directory with Files.createDirectory()

Files.createDirectory(path) is the NIO equivalent of mkdir(): it creates exactly one directory and fails when the parent is missing. Because its failures are checked exceptions, the compiler forces you to handle them, and the exception type identifies the problem — FileAlreadyExistsException when the name is taken and NoSuchFileException when the parent is missing.

import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class CreateDirectoryExample {
    public static void main(String[] args) {
        Path path = Paths.get("logs");
        try {
            Files.createDirectory(path);
            System.out.println("Created directory: " + path);
        } catch (FileAlreadyExistsException e) {
            System.out.println("Already exists: " + path);
        } catch (IOException e) {
            System.out.println("Could not create directory: " + e);
        }
    }
}
Created directory: logs

Catching FileAlreadyExistsException separately keeps the common “already there” case quiet while other IOException subtypes still surface as failures.

Create a directory tree with Files.createDirectories()

Files.createDirectories(path) creates the target and every missing parent in one call, and unlike mkdirs() it returns normally when the directory already exists — the recommended call when you simply need the path to be there. It still throws FileAlreadyExistsException when a regular file occupies the path and other IOException subtypes on real failures such as denied permissions.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class CreateDirectoriesExample {
    public static void main(String[] args) {
        Path path = Paths.get("data/archive/2026");
        try {
            Files.createDirectories(path);
            System.out.println("Directory ready: " + path);
        } catch (IOException e) {
            System.out.println("Could not create directory: " + e);
        }
    }
}
Directory ready: data/archive/2026

Use Apache Commons IO FileUtils.forceMkdir()

FileUtils.forceMkdir() is not part of the JDK; it lives in Apache Commons IO, so the project needs the commons-io dependency (for example, the commons-io:commons-io artifact in Maven or Gradle). Because the library is not on the standard classpath, the call is shown inline rather than as a runnable example:

FileUtils.forceMkdir(new File("data/archive/2026"));

Like createDirectories(), it creates the directory plus any missing parents and returns silently when the directory already exists — despite the name, “force” refers to creating required parents, not to overwriting anything. It throws IOException when a non-directory file occupies the path or the directory cannot be created. Prefer the standard Files methods unless the codebase already uses Commons IO.