Java Reference ยท I/O and Files
Path and Paths
Reference for java.nio.file.Path: create paths with Path.of, resolve, relativize, normalize, and inspect name elements without touching the disk.
A Path (package java.nio.file) is an object that names a location in a file system. It is the modern replacement for the old File class. A Path is purely a value: creating one does not check that the file exists, and none of its methods in this page touch the disk, except toRealPath().
You normally create a path with Path.of(...) (Java 11+). The older Paths.get(...) does the same and still works, but Path.of is the recommended spelling. Paths are immutable, so every operation returns a new path.
Path p = Path.of("reports", "2025", "q1.txt"); Path base = Path.of("/data"); Path full = base.resolve(p);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
static Path of(String first, String... more) static Path of(URI uri) Path resolve(String other) // also resolve(Path) Path relativize(Path other) Path normalize()
| Member | Description |
|---|---|
getFileName() |
The last name element, or null for a root such as /. |
getParent() |
Everything except the last element, or null if there is none. |
getRoot() |
The root component (/ or C:\), or null for a relative path. |
getNameCount(), getName(i), subpath(from, to) |
Access to the name elements. |
resolve(other) |
Joins other onto this path; returns other if it is absolute. |
resolveSibling(other) |
Resolves against the parent: replaces the last element. |
relativize(other) |
Builds the path that leads from this path to other. |
normalize() |
Removes . and redundant .. elements. |
toAbsolutePath() |
Resolves against the current working directory. |
toRealPath() |
Absolute, normalized, symbolic links resolved; checks the file exists. |
toFile(), toUri() |
Conversions to File and URI. |
startsWith, endsWith |
Compare whole name elements, not characters. |
Throws: Path.of throws InvalidPathException (unchecked) for text the file system cannot represent, such as a NUL character. relativize throws IllegalArgumentException if one path is absolute and the other is not. toRealPath() throws IOException (for example NoSuchFileException).
Usage notes
- No disk access.
exists,sizeand friends live onFiles, not onPath. Path.of("a/b")equalsPath.of("a", "b")on systems that use/. On Windows both/and\are accepted as separators, but build paths with several arguments to stay portable.normalize()is textual. It does not look at the file system, soa/link/..becomesaeven iflinkis a symbolic link pointing elsewhere. UsetoRealPath()when links matter.startsWith("rep")is false forreports/x: it compares whole elements, unlikeString.startsWith.- Security: when joining untrusted names onto a base directory, resolve, normalize, and then check
result.startsWith(base)to block..escapes. - Empty path:
Path.of("")is a valid empty path, which refers to the current directory in most operations. - Printing.
toString()uses the platform separator; the output of these examples uses/on the server. Paths.getis a thin legacy wrapper aroundPath.of; both give the same results.
Related
File,Files.readString(),Files.walk(),Files.createTempFile()- Lessons: Files