Let's get started with a Microservice Architecture with Spring Cloud:
How to Get the File Extension of a File in Java
Last updated: January 8, 2024
1. Overview
When working with files in Java, one of the common tasks is determining the file extension.
In this quick tutorial, we’ll explore several ways to obtain the extension of a file in Java.
2. Introduction to the Problem
A file extension is the part of a filename that comes after the last dot (.), such as .txt, .pdf, or .java.
Next, let’s see some examples:
static final String FILE_1 = "MyClass.java"; // filename with an extension
static final String FILE_2 = "fileWithoutExt"; // filename without an extension
static final String FILE_3 = ".gitignore"; // dot file
static final String FILE_4 = "/var/log/app.log"; // full path
static final String FILE_5 = ""; // empty input
As we can see, these five examples cover different cases. Their corresponding file extensions are following:
static final String EXPECTED_1 = "java";
static final String EXPECTED_2 = "";
static final String EXPECTED_3 = "gitignore";
static final String EXPECTED_4 = "log";
static final String EXPECTED_5 = "";
Next, let’s use these inputs to demonstrate how to extract the expected file extensions.
3. Using String Methods
A straightforward way to get the file extension of a file in Java is to use the built-in String methods. As filenames are essentially Strings, we can manipulate the filename String to extract the extension, which is the substring after the last dot:
String getFileExtension(String filename) {
if (filename == null) {
return null;
}
int dotIndex = filename.lastIndexOf(".");
if (dotIndex >= 0) {
return filename.substring(dotIndex + 1);
}
return "";
}
Next, let’s understand how this method works.
To make the method null-safe, we handle the case where the input is null. Then, we use lastIndexOf(“.”) to find the position (dotIndex) of the last dot. Now, we can have two cases:
- Dot is found – Return the substring after it as the file extension
- Input doesn’t contain a dot – Return an empty String, meaning the file has no extension
Next, let’s test this solution using our inputs:
assertNull(getFileExtension(null));
assertEquals(EXPECTED_1, getFileExtension(FILE_1));
assertEquals(EXPECTED_2, getFileExtension(FILE_2));
assertEquals(EXPECTED_3, getFileExtension(FILE_3));
assertEquals(EXPECTED_4, getFileExtension(FILE_4));
assertEquals(EXPECTED_5, getFileExtension(FILE_5));
The test passes if we execute it. So, this solution does the job.
4. Using the Apache Commons IO Library
The Apache Commons IO library provides the handy FilenameUtils.getExtension() method for extracting file extensions.
To use Apache Commons IO, we need to add the library to our project:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.17.0</version>
</dependency>
Of course, we can find the latest library versions in Maven Central.
Using FilenameUtils.getExtension() is pretty straightforward:
assertNull(FilenameUtils.getExtension(null));
assertEquals(EXPECTED_1, FilenameUtils.getExtension(FILE_1));
assertEquals(EXPECTED_2, FilenameUtils.getExtension(FILE_2));
assertEquals(EXPECTED_3, FilenameUtils.getExtension(FILE_3));
assertEquals(EXPECTED_4, FilenameUtils.getExtension(FILE_4));
assertEquals(EXPECTED_5, FilenameUtils.getExtension(FILE_5));
As we can see, we don’t need to manipulate the String inputs manually. The library does everything for us.
5. Using the Guava Library
Like Apache Commons, Guava is another library widely used in Java projects.
In this last approach, we’ll use Guava to find the extension.
Similarly, let’s add the Guava dependency to our pom.xml:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>32.1.3-jre</version>
</dependency>
The latest version of Guava can be found at Maven Central.
Guava’s Files.getFileExtension() allows us to extract the file extension from a String filename conveniently:
assertEquals(EXPECTED_1, Files.getFileExtension(FILE_1));
assertEquals(EXPECTED_2, Files.getFileExtension(FILE_2));
assertEquals(EXPECTED_3, Files.getFileExtension(FILE_3));
assertEquals(EXPECTED_4, Files.getFileExtension(FILE_4));
assertEquals(EXPECTED_5, Files.getFileExtension(FILE_5));
When we give the test a run, it passes. However, it’s worth noting that Guava’s Files.getFileExtension() throws NullPointerException if the input is null:
assertThrows(NullPointerException.class, () -> Files.getFileExtension(null));
That is to say, to avoid the exception, we should perform null checks before passing the input to the method.
6. When the Input Is a File or Path Object
We’ve seen several ways to extract the file extension from a filename or full path. However, all our solutions take String as input.
When we have a File or Path object in hand, there are many ways to get the filename as String from a File or Path, for example, File.getName() or Path.getFilename(). Then, we can use our solutions to get the expected file extensions.
7. Conclusion
In this article, we’ve explored various approaches to get the file extension from a given filename in Java.
We can create a straightforward solution based on the standard String methods. In addition, popular libraries such as Apache Commons IO and Guava offer convenient helper methods to do the job easily.
The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
















