In Java, path.getFileName() returns the last name element of a path as a new Path. For /home/anna/docs/report.txt that is report.txt; for /home/anna/docs it is docs, because a path does not know whether its last part is a file or a folder.
The method only reads the text of the path. The file does not need to exist, and nothing on the disk is checked. If the path has no name elements at all, such as the root /, the result is null.
An instance method of the java.nio.file.Path interface, available since Java 7. Call it on a Path object, as in path.getFileName(...), after import java.nio.file.Path;.
Syntax
Call it on any Path; convert the result with toString() when you need a String:
Java
Path file = Paths.get("/home/anna/docs/report.txt");
Path name = file.getFileName(); // report.txt (a Path)
String text = name.toString(); // "report.txt" (a String)
Call
What it does
p.getFileName()
The last name element, or null for a root or an empty name list.
p.getFileName().toString()
The name as text, ready for String methods such as endsWith.
p.getName(p.getNameCount() - 1)
The same element by index; throws instead of returning null for /.
Since Java 7. A trailing slash is removed when the path is created, so Paths.get("docs/") has the file name docs.
Any single name element by index, not just the last one.
Java Examples
Outputs below are shown for Linux and macOS. On Windows, Java prints \ between names instead of /; the note above each output says when a result really differs.
Example 1 — Get the File Name from Different Paths
Print the last name element of absolute, relative, single-name, folder, and root paths.
Java
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathFileNameBasic {
public static void main(String[] args) {
String[] inputs = {"/home/anna/docs/report.txt", "docs/reports/jan.txt",
"notes.txt", "/home/anna/docs/", "/"};
for (String s : inputs) {
Path p = Paths.get(s);
System.out.println(s + " -> " + p.getFileName());
}
}
}
Output
Run with java PathFileNameBasic. The output is the same on Windows, macOS, and Linux.
Input text is printed as typed. Each line starts with the original String, so the left side looks the same on every system; only the file name on the right comes from Path.
Folders have names too. The trailing slash in /home/anna/docs/ is dropped while parsing, so its last element is docs; Java cannot tell a folder from a file without asking the disk.
The root has no name./ consists only of a root and zero name elements, so getFileName() returns null, which string concatenation prints as the word null.
Example 2 — Read the File Extension from a Path
A small helper takes the text after the last dot of the file name. Hidden files such as .bashrc and names without a dot get an empty extension.
Java
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathFileNameExtension {
static String extension(Path p) {
Path name = p.getFileName();
if (name == null) {
return "";
}
String s = name.toString();
int dot = s.lastIndexOf('.');
return dot > 0 ? s.substring(dot + 1) : "";
}
public static void main(String[] args) {
Path[] files = {Paths.get("photos/beach.JPG"), Paths.get("backup/archive.tar.gz"),
Paths.get("/home/anna/.bashrc"), Paths.get("bin/run")};
for (Path f : files) {
System.out.println(f.getFileName() + " -> [" + extension(f) + "]");
}
}
}
Output
Run with java PathFileNameExtension. The output is the same on Windows, macOS, and Linux.
Name first, then text. The helper asks for the file name, returns an empty string when it is null, and only then converts it with toString(), so the folders in the path can never contain the dot it finds.
Only the last dot counts.lastIndexOf('.') picks the final dot, so archive.tar.gz reports gz; the case is kept, which is why beach.JPG gives JPG (lower-case it before comparing).
Dot at position 0 is not an extension. The dot > 0 test treats .bashrc as a hidden file without an extension, and run has no dot at all, so both print empty brackets.
Example 3 — Find README Files in Any Folder
Pick the paths whose file name is readme.md in any letter case. The root path in the list shows why the null check matters.
Java
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathFileNameReadme {
public static void main(String[] args) {
Path[] paths = {Paths.get("shop/README.md"), Paths.get("shop/src/App.java"),
Paths.get("/"), Paths.get("docs/readme.MD"), Paths.get("readme.txt")};
int found = 0;
for (Path p : paths) {
Path name = p.getFileName();
if (name != null && name.toString().equalsIgnoreCase("readme.md")) {
System.out.println("Found: " + name);
found++;
}
}
System.out.println("README files: " + found);
}
}
Output
Run with java PathFileNameReadme. The output is the same on Windows, macOS, and Linux.
Found: README.md
Found: readme.MD
README files: 2
How It Works
Guard against the root.Paths.get("/") has no file name, so name != null is checked first; without it, name.toString() would throw NullPointerException.
Compare text, not Path objects.equalsIgnoreCase is a String method, so the name is converted with toString() and README.md and readme.MD both match.
Folders do not matter. Only the last element is tested, so the files are found whether they sit in shop or docs, while App.java and readme.txt are skipped.
Frequently Asked Questions
Call path.getFileName(). It returns the last name element as a Path; add toString() when you need a String, for example path.getFileName().toString().
The path has no name elements. That happens for a root such as / on Linux or C:\ on Windows. An ordinary relative or absolute path always has a file name.
Convert the file name to a String and take the text after the last dot: name.substring(name.lastIndexOf('.') + 1). Check that the dot exists and is not the first character, so hidden files like .bashrc are handled.
No. It only looks at the text of the path. Use Files.exists(path) to ask the disk.
Yes. For /home/anna/docs it returns docs. A Path does not know whether the last element is a file or a folder; Files.isDirectory(path) tells you.