Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

Path.of()

Creates a Path object — a path to a file or folder.

Available since: Java 11

Syntax

static Path of(String first, String... more)

Parameters

ParameterTypeDescription
firstStringThe path or its first part.
moreString...The remaining parts of the path; the separator is inserted for you.

Return value

PathA Path object. The file itself is neither created nor checked.

Examples

Parts of a path:

void main() {
    Path path = Path.of("data", "reports", "2025.csv");
    IO.println(path.getFileName());
    IO.println(path.getParent().getFileName());
    IO.println(path.getNameCount());
}

Output:

2025.csv
reports
3

Joining paths and the absolute path:

void main() {
    Path dir = Path.of("logs");
    Path file = dir.resolve("app.log");
    IO.println(file.getFileName());
    IO.println(file.isAbsolute());
    IO.println(file.toAbsolutePath().isAbsolute());
}

Output:

app.log
false
true

Notes

A relative path is resolved against the working directory — where the program was started. Before Java 11 you wrote Paths.get() instead of Path.of() — it works the same. An old java.io.File can be converted with file.toPath().

See also

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.