Java Path Methods

About This Index
This page is the Java Path Methods index: every guide for the methods of the java.nio.file.Path interface, each with syntax, parameters, return value, exceptions, Windows notes, three runnable examples, and FAQs.
Learn what a Path is, how absolute and relative paths differ, and how separators change between systems on the Path hub. Use this page when you need a specific method such as getFileName(), resolve(), or relativize(). All examples compile on Java 8 and later and use Paths.get(...), except the of() page, which needs Java 11.
Nearly every example only builds and inspects paths, so nothing on your disk is touched. The toRealPath() examples create their own temporary files and delete them again.
How to Call a Path Method
Create a Path once, then call instance methods on it. Each method returns a new value; the original path never changes:
import java.nio.file.Path;
import java.nio.file.Paths;
Path report = Paths.get("docs", "reports", "jan.txt"); // Path.of(...) on Java 11+
Path name = report.getFileName(); // jan.txt
Path folder = report.getParent(); // docs/reports
Path feb = report.resolveSibling("feb.txt"); // docs/reports/feb.txt
int parts = report.getNameCount(); // 3 Java Path Methods Table
All method guides in this Java tutorial series (23 tutorials), grouped by purpose.
Inspect the parts of a path
8 methodsRead the file name, the parent folder, the root, single name elements, or a range of them, and check whether a path is absolute.
| Method | Description | Tutorial |
|---|---|---|
getFileName() | Get the last name element of a path, such as report.txt, or null for a root. | Try it |
getParent() | Get the folder part of a path, or null when there is none. | Try it |
getRoot() | Get the root of a path, such as / or C:\, or null for a relative path. | Try it |
getName() | Get one name element of a path by its zero-based index. | Try it |
getNameCount() | Count the name elements in a path; the root is not counted. | Try it |
subpath() | Take a range of name elements out of a path as a new relative path. | Try it |
iterator() | Walk through the name elements of a path one by one as Path objects. | Try it |
isAbsolute() | Check whether a path is complete on its own or relative to the working directory. | Try it |
Compare paths
4 methodsCheck how a path starts or ends, test two paths for equality, and sort paths.
| Method | Description | Tutorial |
|---|---|---|
startsWith() | Check whether a path begins with whole name elements, such as /home/anna. | Try it |
endsWith() | Check whether a path ends with whole name elements, such as "docs/report.txt". | Try it |
equals() | Test whether two Path objects hold the same path text (not whether they point to the same file). | Try it |
compareTo() | Order two paths by their names, for sorting. Returns a negative, zero, or positive int. | Try it |
Build and combine paths
5 methodsCreate paths, join and replace names, compute the way from one path to another, and clean up "." and "..".
| Method | Description | Tutorial |
|---|---|---|
of() | Create a Path from strings or a URI (Java 11+; the same as Paths.get). | Try it |
resolve() | Join a child name or relative path onto a folder path. | Try it |
resolveSibling() | Replace the last name of a path, keeping the same parent folder. | Try it |
relativize() | Build the relative path that leads from one path to another. | Try it |
normalize() | Remove redundant "." and resolvable ".." name elements from a path. | Try it |
Convert paths
6 methodsTurn a path into text, an absolute or real path, a java.io.File, or a URI, and look at its file system.
| Method | Description | Tutorial |
|---|---|---|
toString() | Get the path as text, using the separator of the operating system. | Try it |
toAbsolutePath() | Turn a relative path into an absolute one based on the working directory. | Try it |
toRealPath() | Get the real, existing location of a file, with links and ".." resolved. | Try it |
toFile() | Convert a Path into a java.io.File for older APIs. | Try it |
toUri() | Turn a path into an absolute file: URI, for example to share or store it. | Try it |
getFileSystem() | Get the FileSystem a path belongs to, with its separator and provider. | Try it |
Frequently Asked Questions
Start with getFileName()
Take the last part of a path, such as report.txt, and learn when it is null.
11 people found this page helpful
