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
Call
What 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
Parameter
Description
dir
The folder that must exist when the call returns.
attrs
Optional attributes applied to every folder it creates (not to ones that already existed).
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);
}
}
Output
Run with java CreateDirectoriesNested. The output is the same on Windows, macOS, and Linux.
Returned: reports/2026/q1
q1 is a directory: true
2026 is a directory: true
reports is a directory: true
How It Works
Nothing exists below the temp folder. reports, 2026, and q1 are all missing, so a single createDirectory would fail here.
Parents first, automatically.createDirectories finds the deepest existing ancestor (the temp folder) and creates reports, then 2026, then q1.
Climb back up. The loop starts at q1 and follows getParent() until it reaches the temp folder, printing each level on the way.
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);
}
}
Output
Run with java CreateDirectoriesAgain. The output is the same on Windows, macOS, and Linux.
Second call: no exception
Main.class still there: true
Existing temp folder: also fine
How It Works
A build-style folder. The first call creates build/classes, and a file Main.class is placed inside it.
Repeat without fear. The second call finds classes already present as a directory and returns at once; it never empties or recreates the folder.
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);
}
}
Output
Run with java CreateDirectoriesFileInWay. The output is the same on Windows, macOS, and Linux.
data exists, but it is a file
After removing the file: true
How It Works
A file with no extension.createFile makes a regular file called data, a name that could just as well be a folder.
Relaxed, but not blind.createDirectories accepts an existing directory only; an existing regular file makes it throw FileAlreadyExistsException.
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.