Files.createDirectories()
Creates a folder together with all missing parent folders.
Available since: Java 7
Syntax
static Path createDirectories(Path dir, FileAttribute<?>... attrs) throws IOExceptionParameters
| Parameter | Type | Description |
|---|---|---|
| dir | Path | The path to the folder. |
| attrs | FileAttribute<?>... | Access permissions (on Linux and macOS). Usually omitted. |
Return value
PathThe same path.
Examples
Nested folders in one call; calling it again is not an error:
void main() throws IOException {
Path dir = Path.of("out", "2025", "march");
Files.createDirectories(dir);
Files.createDirectories(dir);
Files.writeString(dir.resolve("report.txt"), "ok");
IO.println(Files.exists(dir.resolve("report.txt")));
}Output:
trueNotes
Files.createDirectory() (without "-ies") creates only one folder and throws if it already exists or the parent is missing. createDirectories() is the better choice almost always.
See also
- Files.exists() — Checks whether a file or folder exists. Related: isDirectory(), isRegularFile(), size().
- Files.writeString() — Writes a string to a file, creating or overwriting it.
- Files.list() — Returns a stream of the files and folders inside a folder (not nested). Files.walk() goes recursively.
- Path.of() — Creates a Path object — a path to a file or folder.
Related recipes
In the tutorial
- Files and paths — Working with the file system in Java — Path, Files.exists, creating folders and files, size, copying, moving, deleting and listing files.