Skip to content
Tutorials Center
My learning

Topics

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.

Java
Path p = Path.of("reports", "2025", "q1.txt");
Path base = Path.of("/data");
Path full = base.resolve(p);

Try it

Java Building and taking apart a path

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.

Java resolve, relativize and normalize

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

Java
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, size and friends live on Files, not on Path.
  • Path.of("a/b") equals Path.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, so a/link/.. becomes a even if link is a symbolic link pointing elsewhere. Use toRealPath() when links matter.
  • startsWith("rep") is false for reports/x: it compares whole elements, unlike String.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.get is a thin legacy wrapper around Path.of; both give the same results.