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
🌐
GeeksforGeeks
geeksforgeeks.org › java › path-getfilename-method-in-java-with-examples
Path getFileName() method in Java with Examples - GeeksforGeeks
July 16, 2019 - Below programs illustrate getFileName() method: Program 1: ... // 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()); } }
🌐
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.
🌐
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.
🌐
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 - Here, the Paths.get() method parses the absolute path and converts it into a Path object. The getFileName() method then extracts the file name from the Path object.
🌐
GeeksforGeeks
geeksforgeeks.org › java › file-getname-method-in-java-with-examples
File getName() method in Java with Examples - GeeksforGeeks
July 11, 2025 - // Java program to demonstrate the // use of getName() function import java.io.*; public class solution { public static void main(String args[]) { // try-catch block to handle exceptions try { // Create a file object File f = new File("c:\\...
🌐
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 ...
🌐
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 ...
Find elsewhere
🌐
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 - import java.nio.file.Path; import ... Path path = Paths.get(absolutePath); // Get the file name using the getFileName() method String fileName = path.getFileName().toString(); // Print the file name System.out.println("File ...
🌐
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.
🌐
Program Creek
programcreek.com › java-api-examples
Java Code Examples for java.nio.file.Path#getFileName()
private void find(final Path path) throws IOException { final Path name = path.getFileName(); if (name != null && matcher.matches(name)) { try (final InputStream inputStream = Files.newInputStream(path)) { final ClassParser classParser = new ClassParser(inputStream, name.toAbsolutePath().toString()); final JavaClass javaClass = classParser.parse(); Assert.assertNotNull(javaClass); } } }
🌐
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 .
🌐
javaspring
javaspring.net › blog › java-get-filename-from-path
Java: Getting Filename from Path — javaspring.net
import java.nio.file.Path; import java.nio.file.Paths; public class GetFilenameUsingPath { public static void main(String[] args) { String filePath = "/home/user/documents/report.pdf"; Path path = Paths.get(filePath); Path fileNamePath = path.getFileName(); if (fileNamePath != null) { String fileName = fileNamePath.toString(); System.out.println("Filename: " + fileName); } } }
🌐
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.
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java stacktraceelement getfilename method
Java StackTraceElement getFileName() Method
February 13, 2026 - The Java StackTraceElement getFileName() method returns the name of the source file containing the execution point represented by this stack trace element.
🌐
GitHub
github.com › Creede15 › practice-it › blob › master › chapter-6 › getFileName.java
practice-it/chapter-6/getFileName.java at master · Creede15/practice-it
* Write a method named getFileName that repeatedly prompts the user for a · * file name until the user types the name of a file that exists on the system. * Once you get a good file name, return that file name as a String.
Author   Creede15
🌐
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); } }...
🌐
Java Guides
javaguides.net › 2024 › 05 › java-get-filename-from-path.html
Java: Get Filename from Path
May 29, 2024 - import java.nio.file.Path; import ... path.getFileName().toString(); } } Paths.get(filePath): Creates a Path object representing the file at the specified path....
🌐
TheLinuxCode
thelinuxcode.com › home › path getfilename() method in java with examples
Path getFileName() Method in Java with Examples – TheLinuxCode
January 27, 2026 - I treat file names like labels on boxes in a warehouse. The label tells you which box you’re holding, but it doesn’t tell you where the box sits. getFileName() returns the label, not the full address.