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 Top answer 1 of 12
352
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));
2 of 12
335
Alternative using Path (Java 7+):
Path p = Paths.get("C:\\Hello\\AnotherFolder\\The File Name.PDF");
String file = p.getFileName().toString();
Note that splitting the string on \\ is platform dependent as the file separator might vary. Path#getName takes care of that issue for you.
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()); } }
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 › 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 ...
Top answer 1 of 5
82
new File(fileName).getName();
or
int idx = fileName.replaceAll("\\\\", "/").lastIndexOf("/");
return idx >= 0 ? fileName.substring(idx + 1) : fileName;
Notice that the first solution is system dependent. It only takes the system's path separator character into account. So if your code runs on a Unix system and receives a Windows path, it won't work. This is the case when processing file uploads being sent by Internet Explorer.
2 of 5
23
new File(absolutePath).getName();
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 ...
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); } } }
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); } }...
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.