The java.util.File getName() method returns the name of the file or directory denoted by this abstract pathname. This method returns the last element in the abstract pathname's name sequence, which is the name of the file or directory.
Example 1:
Suppose we have a file object representing a file named "myfile.txt" as follows:
File f = new File("C:\\Files\\myfile.txt");
Then we can get the name of the file using the getName() method as follows:
String fileName = f.getName();
This will return "myfile.txt" as the name of the file.
Example 2:
Suppose we have a directory object representing a directory named "mydir" as follows:
File dir = new File("C:\\Files\\mydir");
Then we can get the name of the directory using the getName() method as follows:
String dirName = dir.getName();
This will return "mydir" as the name of the directory.
The java.util.File package is a part of the Java Standard Library, which provides classes and interfaces for accessing and manipulating files and directories.
Java File.getName - 30 examples found. These are the top rated real world Java examples of java.util.File.getName extracted from open source projects. You can rate examples to help us improve the quality of examples.