Path getParent() method in Java with Examples Last Updated : 16 Jul, 2019 Comments Improve Suggest changes Like Article Like Report The Java Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a directory.path of an entity could be of two types one is an absolute path and other is a relative path. The absolute path is the location address from the root to the entity while the relative path is the location address which is relative to some other path. getParent() method of java.nio.file.Path used to return the parent path of current path object, or null if this path does not have a parent. The parent path of this path object consists of this path's root component and each element in the path except for the farthest from the root in the directory hierarchy. If this path has more than one element, and no root component, then this method is equivalent to evaluating the expression: subpath(0, getNameCount()-1); Syntax: Path getParent() Parameters: This method accepts nothing. Return value: This method returns a path representing the path's parent. Below programs illustrate getParent() method: Program 1: Java // Java program to demonstrate // java.nio.file.Path.getParent() method import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; public class GFG { public static void main(String[] args) throws IOException { // create object of Path Path path = Paths.get("D:/workspace/AmanCV.docx"); // call getParent() to get parent path Path parentPath = path.getParent(); // print ParentPath System.out.println("Parent Path: " + parentPath); } } Output: Parent Path: D:/workspace Program 2: Java // Java program to demonstrate // java.nio.file.Path.getParent() method import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; public class GFG { public static void main(String[] args) throws IOException { // create object of Path Path path = Paths.get("D:/Resume.pdf"); // call getParent() to get parent path Path parentPath = path.getParent(); // print ParentPath System.out.println("Parent Path: " + parentPath); } } Output: Parent Path: D: References: https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html#getParent() Comment More infoAdvertise with us Next Article Path getParent() method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions Java-Path java.nio.file package Practice Tags : Java Similar Reads Path getRoot() method in Java with Examples The Java Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a di 2 min read Path getNameCount() method in Java with Examples The Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a directo 2 min read Path getName(int) method in Java with Examples The Java Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a di 2 min read Path getFileName() method in Java with Examples The Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a directo 2 min read URL getPath() method in Java with Examples The getPath() function is a part of URL class. The function getPath() returns the Path name of a specified URL. Function Signature: public String getPath() Syntax: url.getPath() Parameter: This function does not require any parameter Return Type: The function returns String Type Below programs illus 2 min read Like