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.
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 ... 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....
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.
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, ...
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 ...
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....
