Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4535

How to Get the File Extension of a File in Java

$
0
0

1. Overview

In this quick tutorial, we’ll show how to obtain the file extension programmatically in Java. We’ll focus on three major approaches to the problem.

In our implementations, the characters after the final ‘.’ will be returned.

Therefore, as a quick example, if our file name is jarvis.txt then it will return the Stringtxt” as the file’s extension.

2. Getting the File Extension

For each approach, we’ll learn how to implement it and follow up with what happens in two special cases:

  • when a filename has no extensions, such as a makefile file
  • and if a filename consists of the extension only, such as .gitignore or .DS_Store.

2.1. Simple String Handling Approach

With this approach, we’ll use a simple String handling approach to finding the extension:

public Optional<String> getExtensionByStringHandling(String filename) {
    return Optional.ofNullable(filename)
      .filter(f -> f.contains("."))
      .map(f -> f.substring(filename.lastIndexOf(".") + 1));
}

This method will check for the dot ‘.’ occurrence in the given filename.

If it exists, then it will find the last position of the dot ‘.’ and return the characters after that, the characters after the last dot ‘.’ known as the file extension.

Special Cases:

  1. No extension – this method will return an empty String
  2. Only extension – this method will return the String after the dot, e.g. “gitignore”

2.2. FilenameUtils.getExtension from Apache Commons IO

In the second approach, we’ll find the extension using a utility class provided by Apache Commons IO library:

public String getExtensionByApacheCommonLib(String filename) {
    return FilenameUtils.getExtension(filename);
}

Here, instead of the file name, we can also specify the full path to a file e.g.C:/baeldung/com/demo.java“.

The method getExtension(String) will check whether the given filename is empty or not.

If filename is empty or null, getExtension(String filename) will return the instance it was given. Otherwise, it returns extension of the filename.

To do this it uses the method indexOfExtension(String) which, in turn, uses lastIndexof(char) to find the last occurrence of the ‘.’. These methods are both provided by FilenameUtils.

This method also checks that there’s no directory separator after the last dot by using another method indexOfLastSeparator(String), which will handle a file in either Unix or Windows format.

Special Cases:

  1. No extension – this method will return an empty string.
  2. Only extension – this method will return the String after the dot, e.g. “gitignore”

2.3. Using the Guava Library

In this last approach, we’ll use Guava library to find the extension.

To add a Guava library, we can add the following dependency to our pom.xml:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>24.1.1-jre</version>
</dependency>

For latest dependency, we can check Maven Central.

After adding the library we can simply use its getFileExtension method:

public String getExtensionByGuava(String filename) {
    return Files.getFileExtension(filename);
}

The method getFileExtension(String) will first check whether the given filename is empty.

If the filename isn’t empty, then it will create a File instance by converting the given filename into an abstract pathname and call File’s getName() method over it, which will return the name of the file denoted by this abstract pathname, or the empty string if the given filename is empty.

Based upon this return value it fetches the index of the last occurrence of the ‘.’ by using String class inbuilt method lastIndexOf(char).

Special Cases:

  1. No extension – this method will return an empty String
  2. Only extension – this method will return the String after the dot, e.g. “gitignore”

3. Conclusion

When picking between Apache Commons and Guava, while both libraries have some common features and they also have functionality that’s absent from their alternative.

This means that if certain functionality is required, pick the one that has it. Otherwise, if more custom scenarios are required, pick the one that does the most of what you need and feel free to wrap it with your own implementation to get the desired result.

Also, check out all the examples in this article on the Github.


Viewing all articles
Browse latest Browse all 4535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>