Java Files createDirectory() Method

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

Definition

In Java, Files.createDirectory(dir) creates exactly one new folder: the last name in dir. Everything before it must already exist, and the folder itself must not.

It is strict on purpose. If anything already has that name, even a folder, it throws FileAlreadyExistsException. When you just want "make sure this folder exists", use createDirectories instead.

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.createDirectory(...), after import java.nio.file.Files;. You never create a Files object.

Syntax

Pass the folder to create; it returns the same path:

Java
Path photos = dir.resolve("photos");
Files.createDirectory(photos);      // creates photos
Files.createDirectory(photos);      // FileAlreadyExistsException
Files.createDirectory(dir.resolve("a/b"));   // NoSuchFileException if a is missing
CallWhat it does
Files.createDirectory(p)Creates one folder; the parent must exist.
Files.createDirectory(p, attr)Also sets attributes, e.g. POSIX permissions, while creating.
Files.createDirectories(p)Creates all missing parents and accepts an existing folder.

Since Java 7. It is the NIO.2 version of java.io.File.mkdir(), which only returns false on failure and never says why.

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

Parameters

ParameterDescription
dirThe folder to create. Its parent folder must already exist.
attrsOptional attributes set atomically while creating, such as PosixFilePermissions.asFileAttribute(...).

Return Value

Return ValueDescription
PathThe dir argument, now naming a new, empty directory.

Compare with Similar Methods

Other ways to make folders:

MethodHow it differs
createDirectories()Creates every missing level and is fine if the folder is already there.
createTempDirectory()Chooses a unique random name in a temp location.
createFile()The same strict rules, but for an empty file.

Java Examples

Example 1 — Create a Folder and Put a File in It

Make a photos folder in the temp folder, check it, and create a file inside.

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

public class CreateDirectoryBasic {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path photos = Files.createDirectory(dir.resolve("photos"));
        System.out.println("Created folder: " + photos.getFileName());
        System.out.println("Is a directory: " + Files.isDirectory(photos));
        Path pic = Files.createFile(photos.resolve("cat.jpg"));
        System.out.println("File inside: " + dir.relativize(pic).toString().replace('\\', '/'));
        Files.delete(pic);
        Files.delete(photos);
        Files.delete(dir);
    }
}

How It Works

  1. Parent exists, child does not. The temp folder is already there and photos is new, which is exactly the case createDirectory is made for.
  2. Use the returned path. The method returns its argument, so the result can be stored straight into photos and used to build cat.jpg.
  3. Delete in reverse. The picture goes first, then photos, then the temp folder; each folder is empty when its turn comes.

Example 2 — An Existing Folder Is Still an Error

Call createDirectory twice on the same name, then compare with createDirectories, which does not complain.

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

public class CreateDirectoryTwice {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path cache = dir.resolve("cache");
        Files.createDirectory(cache);
        try {
            Files.createDirectory(cache);
        } catch (FileAlreadyExistsException e) {
            System.out.println("createDirectory again: FileAlreadyExistsException");
        }
        Files.createDirectories(cache);
        System.out.println("createDirectories again: no error");
        System.out.println("Still a directory: " + Files.isDirectory(cache));
        Files.delete(cache);
        Files.delete(dir);
    }
}

How It Works

  1. Strict second call. The cache folder exists after the first call, and createDirectory treats any existing entry as a conflict, even a folder of the same kind.
  2. Relaxed alternative. createDirectories sees that cache is already a directory and returns quietly.
  3. Choosing between them. Use the strict version when an existing folder means something went wrong (for example, two jobs using one output folder); use the relaxed one for "make sure it exists".

Example 3 — Build a Date Folder Level by Level

Create 2026/11/18 with one createDirectory call per level, and see what happens when a level is skipped.

Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CreateDirectoryLevels {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        String[] parts = {"2026", "11", "18"};
        List<Path> made = new ArrayList<>();
        Path current = dir;
        for (String part : parts) {
            current = Files.createDirectory(current.resolve(part));
            made.add(current);
            System.out.println("Made " + dir.relativize(current).toString().replace('\\', '/'));
        }
        try {
            Files.createDirectory(dir.resolve("a").resolve("b"));
        } catch (NoSuchFileException e) {
            System.out.println("a/b failed: parent a does not exist");
        }
        Collections.reverse(made);
        for (Path p : made) {
            Files.delete(p);
        }
        Files.delete(dir);
    }
}

How It Works

  1. Walk downwards. Each loop pass resolves the next name against the folder made in the previous pass, so every parent exists by the time its child is created.
  2. Remember what you made. The new folders are collected in a list in creation order: 2026, then 11, then 18.
  3. A skipped level fails. Asking for a/b while a is missing throws NoSuchFileException; nothing is created.
  4. Reverse for cleanup. Collections.reverse flips the list so 18 is deleted before 11 and 11 before 2026.

Frequently Asked Questions

No. It creates only the last folder in the path, and the call fails, typically with NoSuchFileException, if a parent is missing. Use Files.createDirectories for that.
Because it promises that it created a new folder. Any existing entry with that name, including a directory, is reported as FileAlreadyExistsException.
createDirectory creates one folder and fails if it exists or its parent is missing. createDirectories creates all missing parents and does nothing if the folder already exists.
The same Path you passed in, which lets you write Path photos = Files.createDirectory(dir.resolve("photos")).
They do the same job, but File.mkdir returns false on failure without a reason. Files.createDirectory throws a specific exception, so you can tell an existing folder from a missing parent.

Next: Files.createFile()

Create a new, empty file, failing if anything with that name already exists.

createFile() 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.

9 people found this page helpful