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
Call
What 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
Parameter
Description
dir
The folder to create. Its parent folder must already exist.
attrs
Optional attributes set atomically while creating, such as PosixFilePermissions.asFileAttribute(...).
Return Value
Return Value
Description
Path
The dir argument, now naming a new, empty directory.
Run with java CreateDirectoryBasic. The output is the same on Windows, macOS, and Linux.
Created folder: photos
Is a directory: true
File inside: photos/cat.jpg
How It Works
Parent exists, child does not. The temp folder is already there and photos is new, which is exactly the case createDirectory is made for.
Use the returned path. The method returns its argument, so the result can be stored straight into photos and used to build cat.jpg.
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);
}
}
Output
Run with java CreateDirectoryTwice. The output is the same on Windows, macOS, and Linux.
createDirectory again: FileAlreadyExistsException
createDirectories again: no error
Still a directory: true
How It Works
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.
Relaxed alternative.createDirectories sees that cache is already a directory and returns quietly.
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);
}
}
Output
Run with java CreateDirectoryLevels. The output is the same on Windows, macOS, and Linux.
Made 2026
Made 2026/11
Made 2026/11/18
a/b failed: parent a does not exist
How It Works
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.
Remember what you made. The new folders are collected in a list in creation order: 2026, then 11, then 18.
A skipped level fails. Asking for a/b while a is missing throws NoSuchFileException; nothing is created.
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.