just use File.getName()

File f = new File("C:\\Hello\\AnotherFolder\\The File Name.PDF");
System.out.println(f.getName());

using String methods:

  File f = new File("C:\\Hello\\AnotherFolder\\The File Name.PDF");  
System.out.println(f.getAbsolutePath().substring(f.getAbsolutePath().lastIndexOf(File.separator)+1));
Answer from PermGenError on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java io › getting the filename from a string containing an absolute file path
Getting the Filename From a String Containing an Absolute File Path | Baeldung
January 8, 2024 - Once we have a Path object, we can get the filename by calling the Path.getFileName() method. Unlike the File class, we can create a Path instance using the static Paths.get() method.
🌐
GeeksforGeeks
geeksforgeeks.org › java › path-getfilename-method-in-java-with-examples
Path getFileName() method in Java with Examples - GeeksforGeeks
July 16, 2019 - Return value: This method returns the name of the file or directory pointed by this path object. Below programs illustrate getFileName() method: Program 1: ... // Java program to demonstrate // java.nio.file.Path.getFileName() method import ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › nio › file › Path.html
Path (Java Platform SE 8 )
April 21, 2026 - A Path is considered to be an empty path if it consists solely of one name element that is empty. Accessing a file using an empty path is equivalent to accessing the default directory of the file system. Path defines the getFileName, getParent, getRoot, and subpath methods to access the path ...
🌐
Java2s
java2s.com › Tutorials › Java › java.nio.file › Path › Java_Path_getFileName_.htm
Java Tutorial - Java Path.getFileName()
In the following code shows how to use Path.getFileName() method. import java.nio.file.Path; import java.nio.file.Paths; /*from w w w .
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › nio › file › Path.html
Path (Java Platform SE 7 )
A Path is considered to be an empty path if it consists solely of one name element that is empty. Accessing a file using an empty path is equivalent to accessing the default directory of the file system. Path defines the getFileName, getParent, getRoot, and subpath methods to access the path ...
🌐
Tutorialspoint
tutorialspoint.com › java › io › file_getname.htm
Java - File getName() method
The Java File getName() method returns the last name of the pathname's name sequence, that means the name of the file or directory denoted by this abstract path name is returned.
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › io › pathOps.html
Path Operations (The Java™ Tutorials > Essential Java Classes > Basic I/O)
// Microsoft Windows syntax Path path = Paths.get("C:\\home\\joe\\foo"); // Solaris syntax Path path = Paths.get("/home/joe/foo"); System.out.format("toString: %s%n", path.toString()); System.out.format("getFileName: %s%n", path.getFileName()); System.out.format("getName(0): %s%n", path.getName(0)); System.out.format("getNameCount: %d%n", path.getNameCount()); System.out.format("subpath(0,2): %s%n", path.subpath(0,2)); System.out.format("getParent: %s%n", path.getParent()); System.out.format("getRoot: %s%n", path.getRoot());
🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › get-the-name-of-the-file-and-path-in-java
Get the name of the file and path in Java
July 30, 2019 - The getPath() returns the abstract pathname in the form of a pathname string. A program that demonstrates this is given as follows − ... import java.io.File; public class Demo { public static void main(String[] args) { File file = new File("C:" + File.separator + "jdk11.0.2" + File.separator, ...
🌐
HappyCoders.eu
happycoders.eu › java › file-and-directory-names-file-path-paths
File and Directory Names in Java: File, Path, Paths
November 29, 2024 - There are no variants of these methods that return a String. The only way to convert a Path object into a String is to call its toString() method. The getFileName() method returns the name of the file or directory.
🌐
Vultr Docs
docs.vultr.com › java › examples › get-the-name-of-the-file-from-the-absolute-path
Java Program to Get the name of the file from the absolute path | Vultr Docs
April 10, 2025 - The Path class, combined with the ... string representation of the path into a Path object. Obtain the name of the file by calling getFileName() method on the Path object....
🌐
Mkyong
mkyong.com › home › java › how to get the filepath of a file in java
How to get the filepath of a file in Java - Mkyong.com
August 24, 2020 - Below is the mapping of File and Path. ... For java.nio.file.Path, we can use the following APIs to get the file path of a file.
🌐
Programiz
programiz.com › java-programming › examples › get-file-name-from-absolute-path
Java Program to Get the name of the file from the absolute path
import java.io.File; class Main ... Bhandari\\Desktop\\Programiz\\Java Article\\Test.class"); // get file name using getName() String fileName = file.getName(); System.out.println("File Name: " + fileName); } } ... In the above example, we have used the getName() method of ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › path-getfilename-method-in-java-with-examples
Path getFileName() method in Java with Examples - GeeksforGeeks
July 16, 2019 - Return value: This method returns the name of the file or directory pointed by this path object. Below programs illustrate getFileName() method: Program 1: ... // Java program to demonstrate // java.nio.file.Path.getFileName() method import ...
🌐
PREP INSTA
prepinsta.com › home › java tutorial › java program to get file name from absolute path
Java Program to Get File Name from Absolute Path | Prepinsta
September 15, 2023 - Explanation : In this example, a Path object is created using the file's absolute path using the Paths class. The file name is then obtained and put in the fileName variable by calling the getFileName() function on the Path object.
🌐
Wikitechy
wikitechy.com › tutorials › java › how-to-get-file-name-and-file-path-in-java
How to Get file name and file path in Java - By Microsoft Awarded MVP - Learn in 30sec | wikitechy
How to Get file name and file path in Java - In Java File class a member method getName() returns the name of the file or directory denoted by this abstract path name, or the empty string if this path-name’s name sequence is empty and getPath() method returns the path of a file.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › nio › file › Path.html
Path (Java SE 21 & JDK 21)
January 20, 2026 - A Path is considered to be an empty ... the default directory of the file system. Path defines the getFileName, getParent, getRoot, and subpath methods to access the path components or a subsequence of its name elements....
🌐
javaspring
javaspring.net › blog › java-get-filename-from-path
Java: Getting Filename from Path — javaspring.net
It provides a simple way to work with files and directories, including getting the filename from a path. import java.io.File; public class GetFilenameUsingFile { public static void main(String[] args) { String filePath = "/home/user/documen...