Related Articles

Related Articles

Path getFileName() method in Java with Examples
  • Last Updated : 16 Jul, 2019

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 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.

getFileName() method of java.nio.file.Path used to return the name of the file or directory pointed by this path object. The file name is the farthest element from the root in the directory hierarchy.

Syntax:

Path getFileName()

Parameters: This method accepts nothing.

Return value: This method returns the name of the file or directory pointed by this path object.



Below programs illustrate getFileName() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// java.nio.file.Path.getFileName() 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 getFileName() and get FileName path object
        Path fileName = path.getFileName();
  
        // print FileName
        System.out.println("FileName: "
                           + fileName.toString());
    }
}

chevron_right


Output:

FileName: AmanCV.docx

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// java.nio.file.Path.getFileName() 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 getFileName() and get FileName path object
        Path fileName = path.getFileName();
  
        // print FileName
        System.out.println("FileName: "
                           + fileName.toString());
    }
}

chevron_right


Output:

FileName: Resume.pdf

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :