In Java, path.getParent() returns the path without its last name element: the folder that would contain it. For /home/anna/docs/report.txt the parent is /home/anna/docs, and for docs/report.txt it is docs.
It returns null when there is no parent in the text: a single relative name such as report.txt, or a root such as /. The parent of /home is the root /. Like every Path method here, it only works on the text.
An instance method of the java.nio.file.Path interface, available since Java 7. Call it on a Path object, as in path.getParent(...), after import java.nio.file.Path;.
Syntax
Call it on a path and check the result for null before using it:
Same names as the parent, but always relative (no root).
Java Examples
Outputs below are shown for Linux and macOS. On Windows, Java prints \ between names instead of /; the note above each output says when a result really differs.
Example 1 — Get the Parent Folder of Several Paths
See the parent of a file deep in a folder, a relative path, a bare name, a top-level folder, and the root.
Java
import java.nio.file.Paths;
public class PathParentBasic {
public static void main(String[] args) {
String[] inputs = {"/home/anna/docs/report.txt", "docs/report.txt",
"report.txt", "/home", "/"};
for (String s : inputs) {
System.out.println(s + " -> " + Paths.get(s).getParent());
}
}
}
Output
Run with java PathParentBasic. Shown for Linux and macOS. On Windows the first parent prints as \home\anna\docs and the parent of /home as \; the other lines are the same.
The loop variable is a Path.for (Path p = start; p != null; p = p.getParent()) replaces the current path with its parent on every turn.
One name shorter each time. Each step removes one element from the end, so the four names produce four lines, from the file up to the top folder projects.
null ends the walk. projects is a single relative name, so its parent is null and the loop stops; an absolute start would print the root / as one extra level.
Example 3 — See Why getParent() Does Not Resolve ".."
Compare getParent() with normalize().getParent() for paths that contain . and ...
Java
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathParentDots {
public static void main(String[] args) {
String[] inputs = {"docs/./report.txt", "docs/old/../report.txt", "../report.txt"};
for (String s : inputs) {
Path p = Paths.get(s);
System.out.println(s);
System.out.println(" getParent(): " + p.getParent());
System.out.println(" normalize().getParent(): " + p.normalize().getParent());
}
}
}
Output
Run with java PathParentDots. Shown for Linux and macOS. Windows prints the same paths with \ between the names.
Dots are just names. To getParent(), . and .. are ordinary name elements, so it simply removes report.txt and leaves them in place.
Clean first for the real folder.normalize() removes the . and cancels old against .., so both of the first paths have the parent docs.
A leading ".." stays. In a relative path there is no folder in front of .. to cancel, so the parent of ../report.txt is .. either way.
Frequently Asked Questions
Call path.getParent(). For /home/anna/docs/report.txt it returns /home/anna/docs. Check the result for null before you use it.
The path text has nothing in front of its last name, for example report.txt, or it is a root such as /. Call toAbsolutePath() first if you need the real folder of a bare file name.
No. It removes the last name element from the text and nothing else. Use normalize() to resolve "." and "..", and toRealPath() to follow links on the disk.
Chain two calls: path.getParent().getFileName(). For /home/anna/docs/report.txt that is docs.
The root path /. The root itself has no parent, so Paths.get("/").getParent() is null.