Java Path Methods

Beginner
5 min read
Updated: Nov 2026
23 method guides
Method index

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.

Path methods work on names, not files.

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:

Java
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 methods

Read the file name, the parent folder, the root, single name elements, or a range of them, and check whether a path is absolute.

MethodDescriptionTutorial
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 methods

Check how a path starts or ends, test two paths for equality, and sort paths.

MethodDescriptionTutorial
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 methods

Create paths, join and replace names, compute the way from one path to another, and clean up "." and "..".

MethodDescriptionTutorial
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 methods

Turn a path into text, an absolute or real path, a java.io.File, or a URI, and look at its file system.

MethodDescriptionTutorial
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

Methods to read the parts of a path (getFileName, getParent, getRoot, getName, getNameCount, subpath, iterator, isAbsolute), to compare paths (startsWith, endsWith, equals, compareTo), to build paths (of, resolve, resolveSibling, relativize, normalize), and to convert them (toString, toAbsolutePath, toRealPath, toFile, toUri, getFileSystem).
This index lists 23 guides, from compareTo() to toUri(), grouped into four categories.
Almost never. A Path is only a name, so most methods just work on text. toRealPath() checks the disk, and toAbsolutePath() and toUri() read the current working directory. To create, read, or delete files, pass the Path to the Files class.
Only the static factory Path.of(). Every other method exists since Java 7. Before Java 11, use Paths.get(), which does the same job.
Read the Java Path hub first. Then try getFileName(), getParent(), resolve(), and normalize() from the tables below.

Start with getFileName()

Take the last part of a path, such as report.txt, and learn when it is null.

getFileName() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful