The Wayback Machine - https://web.archive.org/web/20240121164629/https://www.geeksforgeeks.org/path-getroot-method-in-java-with-examples/
Open In App
Related Articles

Path getRoot() method in Java with Examples

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
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.

getRoot() method of java.nio.file.Path used to return path object of the root component of this path object or null if this path does not have a root component.

Syntax:

Path getRoot()

Parameters: This method accepts nothing.

Return value: This method returns a path representing the root component of this path, or null.

Below programs illustrate getRoot() method:
Program 1:




// Java program to demonstrate
// java.nio.file.Path.getRoot() 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:\\eclipse");
  
        // call getRoot() to get root path
        Path root = path.getRoot();
  
        // print Root Path
        System.out.println("Root Path: "
                           + root);
    }
}


Output:

Program 2:




// Java program to demonstrate
// java.nio.file.Path.getRoot() 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("C:/Resume.pdf");
  
        // call getRoot() to get root path
        Path root = path.getRoot();
  
        // print RootPath
        System.out.println("Root Path: "
                           + root);
    }
}


Output:

References: https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html#getRoot()


Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 16 Jul, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials