Open In App

Path getName(int) method in Java with Examples

Last Updated : 01 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
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.
getName(int index) method of java.nio.file.Path used to return a name element of this path as a Path object. We passed the index as parameter and index represent the index of the name element to return. The element that is closest to the root in the directory hierarchy has index 0. The element that is farthest from the root has index count-1.
Syntax: 
 

Path getName(int index)

Parameters: This method accepts a single parameter index which is the index of the element.
Return value: This method returns the name element.
Exception: This method throw and Exception IllegalArgumentException if the index is negative, an index is greater than or equal to the number of elements, or this path has zero name elements.
Below programs illustrate getName(int index) method: 
Program 1: 
 

Java




// Java program to demonstrate
// java.nio.file.Path.getName(int index) 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:\\temp\\DS and algo"
                        + "\\algorithm and data struc"
                        + "\\Problem sets");
 
        // call getName(int i) to get the
        // element at index i
        Path indexpath = path.getName(3);
 
        // print ParentPath
        System.out.println("Index 3: "
                           + indexpath);
    }
}


Output: 

Index 3: Problem sets

 

Program 2: 
 

Java




// Java program to demonstrate
// java.nio.file.Path.getName(int index) 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\\configuration"
                        + "\\org.eclipse.update");
 
        // call getName(int i) to get
        // the element at index i
        Path indexpath = path.getName(2);
 
        // print ParentPath
        System.out.println("Index 2: "
                           + indexpath);
    }
}


Output: 

Index 2: org.eclipse.update

 

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads