Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

Files.createDirectories()

Creates a folder together with all missing parent folders.

Available since: Java 7

Syntax

static Path createDirectories(Path dir, FileAttribute<?>... attrs) throws IOException

Parameters

ParameterTypeDescription
dirPathThe path to the folder.
attrsFileAttribute<?>...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:

true

Notes

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.

Thanks for reading!

Donate to the site — we work hard for you.