Java File getName() Method
Example
Get the name of a file:
import java.io.File;
public class Main {
public static void main(String[] args) {
File myFile = new File("myFolder/filename.txt");
System.out.println(myFile.getName());
}
}
Definition and Usage
The getName() method returns the name of the file or directory.
It returns only the last part of the path. For the path myFolder/filename.txt, the name is filename.txt. The file does not need to exist.
Syntax
public String getName()
Parameter Values
None.
Technical Details
| Returns: | The last part of the path, as a String. |
|---|
Related Pages
❮ File Methods