Java Files createDirectories() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

In Java, Files.createDirectories(dir) makes sure dir exists as a folder, creating every missing parent on the way, much like mkdir -p on Linux. One call can turn nothing into reports/2026/q1.

If the folder is already there it simply returns. It only fails when something is in the way, for example when that name already exists as a regular file, or when permissions forbid creating a level.

A static method of the final utility class java.nio.file.Files, available since Java 7. Call it on the class name, as in Files.createDirectories(...), after import java.nio.file.Files;. You never create a Files object.

Syntax

Pass the deepest folder you need:

Java
Path q1 = dir.resolve("reports").resolve("2026").resolve("q1");
Files.createDirectories(q1);   // creates reports, 2026, and q1
Files.createDirectories(q1);   // already exists: no error
CallWhat it does
Files.createDirectories(p)Creates p and all missing parents.
Files.createDirectories(file.getParent())Common idiom before writing a file into a new folder.
Files.createDirectory(p)Strict single-level version.

Since Java 7. It is the NIO.2 version of java.io.File.mkdirs(). The returned path may be the absolute form of the argument when parents had to be created.

Library declarations
Java
public static Path createDirectories(Path dir, FileAttribute<?>... attrs) throws IOException

Parameters

ParameterDescription
dirThe folder that must exist when the call returns.
attrsOptional attributes applied to every folder it creates (not to ones that already existed).

Return Value

Return ValueDescription
PathThe directory, now guaranteed to exist.

Compare with Similar Methods

Folder-creating methods side by side:

MethodHow it differs
createDirectory()One level only; fails if it exists or the parent is missing.
createTempDirectory()Always a brand-new folder with a random name.
isDirectory()Only checks; never creates anything.

Java Examples

Example 1 — Create reports/2026/q1 in One Call

Create a three-level folder path, then walk back up and check each level.

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

public class CreateDirectoriesNested {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path q1 = dir.resolve("reports").resolve("2026").resolve("q1");
        Path made = Files.createDirectories(q1);
        System.out.println("Returned: " + dir.relativize(made).toString().replace('\\', '/'));
        for (Path p = q1; !p.equals(dir); p = p.getParent()) {
            System.out.println(p.getFileName() + " is a directory: " + Files.isDirectory(p));
        }
        Files.delete(q1);
        Files.delete(q1.getParent());
        Files.delete(q1.getParent().getParent());
        Files.delete(dir);
    }
}

How It Works

  1. Nothing exists below the temp folder. reports, 2026, and q1 are all missing, so a single createDirectory would fail here.
  2. Parents first, automatically. createDirectories finds the deepest existing ancestor (the temp folder) and creates reports, then 2026, then q1.
  3. Climb back up. The loop starts at q1 and follows getParent() until it reaches the temp folder, printing each level on the way.
  4. Three deletes for three levels. q1.getParent() is 2026 and its parent is reports, deleted in that order before the temp folder.

Example 2 — Call It as Often as You Like

Calling createDirectories on a folder that already exists does nothing, and the files inside are kept.

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

public class CreateDirectoriesAgain {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path classes = dir.resolve("build").resolve("classes");
        Files.createDirectories(classes);
        Path main = Files.createFile(classes.resolve("Main.class"));
        Files.createDirectories(classes);
        System.out.println("Second call: no exception");
        System.out.println("Main.class still there: " + Files.exists(main));
        Files.createDirectories(dir);
        System.out.println("Existing temp folder: also fine");
        Files.delete(main);
        Files.delete(classes);
        Files.delete(classes.getParent());
        Files.delete(dir);
    }
}

How It Works

  1. A build-style folder. The first call creates build/classes, and a file Main.class is placed inside it.
  2. Repeat without fear. The second call finds classes already present as a directory and returns at once; it never empties or recreates the folder.
  3. Any existing folder works. Even the temp folder itself is accepted, which is why programs call this method at start-up without checking first.

Example 3 — When the Name Belongs to a File

If the path already exists as a regular file, createDirectories cannot turn it into a folder and throws FileAlreadyExistsException.

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

public class CreateDirectoriesFileInWay {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path data = Files.createFile(dir.resolve("data"));
        try {
            Files.createDirectories(data);
        } catch (FileAlreadyExistsException e) {
            System.out.println("data exists, but it is a file");
        }
        Files.delete(data);
        Files.createDirectories(data);
        System.out.println("After removing the file: " + Files.isDirectory(data));
        Files.delete(data);
        Files.delete(dir);
    }
}

How It Works

  1. A file with no extension. createFile makes a regular file called data, a name that could just as well be a folder.
  2. Relaxed, but not blind. createDirectories accepts an existing directory only; an existing regular file makes it throw FileAlreadyExistsException.
  3. Clear the way. After the file is deleted the same call succeeds, and isDirectory confirms data is now a folder.

Frequently Asked Questions

No. If the directory already exists it returns without an error. It only throws FileAlreadyExistsException when the path exists as something other than a directory, such as a regular file.
Very close. Both create every missing parent and accept an existing directory.
No. Parents are created one by one, so if a later level fails, the earlier ones remain. createDirectory is atomic for its single folder.
Call Files.createDirectories(file.getParent()) and then write the file. getParent() returns null for a bare file name, so check that case if the path may be relative.
They do the same job, but File.mkdirs only returns true or false. Files.createDirectories throws an IOException that explains why it failed.

Next: Files.createDirectory()

Create one new folder whose parent already exists; fails if the name is taken.

createDirectory() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful