Java Path getParent() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

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:

Java
Path file = Paths.get("/home/anna/docs/report.txt");
Path folder = file.getParent();          // /home/anna/docs
Path none = Paths.get("report.txt").getParent();   // null
CallWhat it does
p.getParent()The path minus its last name, or null.
p.getParent().getFileName()The name of the folder that holds the file, for example docs.
p.normalize().getParent()The parent after . and .. are cleaned up.

Since Java 7. The method does not look at the disk and does not resolve .., so Paths.get("a/..").getParent() is a.

Library declarations
Java
// java.nio.file.Path (interface)
Path getParent()

Parameters

ParameterDescription
(none)No arguments; the parent of the path the method is called on is returned.

Return Value

Return ValueDescription
PathThe root (if any) plus all name elements except the last one.
nullThere is no parent: a single relative name, a root, or an empty path.

Compare with Similar Methods

Related ways to get part of a path:

MethodHow it differs
getFileName()The last element, the part getParent() removes.
getRoot()Just the root; the parent of /home equals it.
subpath(0, n - 1)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());
        }
    }
}

How It Works

  1. Drop the last name. For the first two inputs the parent is everything before report.txt, keeping the root when there is one.
  2. Nothing left means null. report.txt has a single relative name, and removing it leaves nothing, so the result is null rather than an empty path.
  3. The root is a parent, but has none. The only thing in front of home is the root, so /home reports /; asking the root itself gives null.

Example 2 — Walk Up from a Source File to the Top Folder

Start at a source file and call getParent() in a loop until it returns null, printing each level with its depth.

Java
import java.nio.file.Path;
import java.nio.file.Paths;

public class PathParentWalkUp {
    public static void main(String[] args) {
        Path start = Paths.get("projects/shop/src/Main.java");
        int level = 0;
        for (Path p = start; p != null; p = p.getParent()) {
            System.out.println(level + ": " + p);
            level++;
        }
        System.out.println("Levels: " + level);
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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());
        }
    }
}

How It Works

  1. Dots are just names. To getParent(), . and .. are ordinary name elements, so it simply removes report.txt and leaves them in place.
  2. Clean first for the real folder. normalize() removes the . and cancels old against .., so both of the first paths have the parent docs.
  3. 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.

Next: Path.getRoot()

Get the root of a path, such as / or C:\, or null for a relative path.

getRoot() 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.

14 people found this page helpful