How to get extension of a File in Java

July 16, 2020 No comments Java IO Java File Extension QA

1. Introduction

In this tutorial, we are going to present several ways to get the extension of a File using plain Java and libraries such as Guava or Apache Commons IO.

To test different approaches and solutions we create an empty text file under the path: /tmp/test.txt.

2. Get file extension using filter method from Java 8

Let's start with the first approach in plain Java:

package com.frontbackend.java.io.extension;

import java.io.File;
import java.util.Optional;

public class GetFileExtensionUsingFilter {

    public static void main(String[] args) {

        File source = new File("/tmp/test.txt");
        String filename = source.getName();

        String extension = Optional.of(filename)
                                   .filter(f -> f.contains("."))
                                   .map(f -> f.substring(filename.lastIndexOf(".") + 1))
                                   .orElse("");

        System.out.println(extension);
    }
}

This solution first checks if a filename contains the dot . character. Then returns all characters after the last occurrence of the dot ..

This method will return an empty string if no extension found.

3. Obtain file extension with Apache Commons IO library

The Apache Commons IO library provides a special utility method to find filename extension. Let's check the following solution:

package com.frontbackend.java.io.extension;

import java.io.File;

import org.apache.commons.io.FilenameUtils;

public class GetFileExtensionUsingFilenameUtils {

    public static void main(String[] args) {

        File source = new File("/tmp/test.txt");
        String filename = source.getName();

        System.out.println(FilenameUtils.getExtension(filename)); // "txt"
        System.out.println(FilenameUtils.getExtension("test")); // ""
    }
}

In this example, we used FilenameUtils.getExtension(...) method that is the first step checks if given String is empty. Then with lastIndexOf(...) function it finds the last occurrence of the dot character and returns all characters after that dot.

When filename does not contain any extension, FilenameUtils.getExtension(...) will return an empty String.

4. Use Guava to get file extension

The last approach use Files utility class from Guava library:

package com.frontbackend.java.io.extension;

import java.io.File;

import com.google.common.io.Files;

public class GetFileExtensionUsingFiles {

    public static void main(String[] args) {

        File source = new File("/tmp/test.txt");
        String filename = source.getName();

        System.out.println(Files.getFileExtension(filename)); // "txt"
    }
}

This solution is very similar to Apache Commons IO. Files.getFileExtension(...) method will do all necessary work for us and return the file extension or an empty string if the file does not have an extension.

5. Conclusion

In this short article, we presented various ways to obtain file extension in Java. All approaches based on filename and we can simply implement one using lastIndexOf and contains methods.

{{ message }}

{{ 'Comments are closed.' | trans }}