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
| Parameter | Type | Description |
|---|---|---|
| first | String | The path or its first part. |
| more | String... | 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
3Joining 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
trueNotes
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
- Files.readString() — Reads a whole text file into one string.
- Files.exists() — Checks whether a file or folder exists. Related: isDirectory(), isRegularFile(), size().
- Files.createDirectories() — Creates a folder together with all missing parent folders.
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.