Java
Filename
File Extension
Programming
Coding Tips

How to get a filename without the extension in Java

Codemia Knowledge Hub ·

Take the last path segment with Path.getFileName(), then cut it at the last dot with lastIndexOf('.') and substring. If Apache Commons IO is already a dependency, FilenameUtils.getBaseName does both steps in one call. Decide up front how to handle names with several dots, no dot, or a leading dot.

Isolate the file name first

When you start from a full path, extract the last segment before removing the extension. In modern Java, use Path:

java
1import java.nio.file.Path;
2
3Path path = Path.of("/tmp/reports/summary.csv");
4String fileName = path.getFileName().toString();
5
6System.out.println(fileName); // summary.csv

Working only on the file name stops a dot in a directory name from being mistaken for the extension separator.

Remove the last extension with lastIndexOf

In plain Java, find the last dot and keep what comes before it:

java
1public static String baseName(String fileName) {
2    int dot = fileName.lastIndexOf('.');
3    if (dot <= 0) {
4        return fileName;
5    }
6    return fileName.substring(0, dot);
7}
8
9public static void main(String[] args) {
10    System.out.println(baseName("report.pdf"));
11    System.out.println(baseName("archive.tar.gz"));
12    System.out.println(baseName("README"));
13}

This prints:

text
report
archive.tar
README

Only the final extension is removed. That is usually the intent, since "archive.tar.gz" is commonly treated as a file named "archive.tar" with a "gz" extension.

Combine Path with a helper

java
1import java.nio.file.Path;
2
3public class FileNames {
4    public static String baseName(Path path) {
5        String fileName = path.getFileName().toString();
6        int dot = fileName.lastIndexOf('.');
7        if (dot <= 0) {
8            return fileName;
9        }
10        return fileName.substring(0, dot);
11    }
12
13    public static void main(String[] args) {
14        Path path = Path.of("images/photo.backup.jpg");
15        System.out.println(baseName(path));
16    }
17}

Keeping path handling and string handling separate makes the code easier to test.

Apache Commons IO FilenameUtils.getBaseName

java
1import org.apache.commons.io.FilenameUtils;
2
3String base = FilenameUtils.getBaseName("/tmp/reports/summary.csv");
4System.out.println(base);

This is convenient when Commons IO is already in the codebase. It is usually not worth adding the library just for this.

Hidden files and multiple dots

Not every dot marks an extension. Unix-style hidden files such as .gitignore or .env start with a dot without having a base name plus extension in the usual sense, which is why the helper returns the whole string when the dot is at position 0. Code that cuts at the first dot instead turns these names into empty strings.

Also decide what "archive.tar.gz" should become. Some systems remove only "gz", others treat "tar.gz" as a compound extension. Removing only the final suffix with lastIndexOf is the safest default unless your domain needs special rules, and files with several dots or none at all should get explicitly defined behavior rather than guesswork.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms