HOWTO · Java

How to Get the File Extension of a File in Java

This article introduces how to get the file extension of a file in Java.

On this page

A file extension is the suffix after the last dot in a name, such as pdf in student-records.pdf. Java has no dedicated extension getter on File or String, so you either call a utility like FilenameUtils from Apache Commons IO or locate the last dot yourself with String methods. Both approaches are shown below, along with the rules that keep them correct on full paths.

Get a file extension with Apache Commons IO

FilenameUtils.getExtension() returns the text after the last dot in a file name or path. It already knows about both / and \ separators, so a dot inside a directory name is never mistaken for an extension, and it returns "" when the name has no usable extension.

The class is not part of the JDK, so add the commons-io dependency to your project through Maven or Gradle, or place the JAR on the classpath. After adding the dependency, import org.apache.commons.io.FilenameUtils and call String extension = FilenameUtils.getExtension(fileName);. With fileName set to student-records.pdf, it returns pdf.

Because the external library is required at compile time, this API call is shown inline; the runnable examples below use only the JDK.

Extract an extension with Java string methods

No library is needed for a plain file name: lastIndexOf('.') finds the last dot and substring() takes everything after it.

Run this example:

public class SimpleTesting {
  public static void main(String[] args) {
    String fileName = "student-records.pdf";
    int dot = fileName.lastIndexOf('.');
    String extension = dot > 0 ? fileName.substring(dot + 1) : "";
    System.out.println("File extension is : " + extension);
  }
}

Output:

File extension is : pdf

The dot > 0 check covers two cases where there is no extension to report: readme produces "" because lastIndexOf returns -1, and a leading dot such as in .env is treated as a name marker rather than an extension separator. What this short form cannot see is whether the dot sits in a directory part of the name — that needs the path-aware version next.

Handle paths and edge cases

On a full path the extension dot must come after the last path separator. The helper below checks both / and \, so a dotted folder such as data.d does not leak into the result. It returns "" for a missing dot, a leading-dot name, and a name that ends in a dot:

public class SimpleTesting {

  static String fileExtension(String path) {
    if (path == null) {
      return "";
    }
    int dot = path.lastIndexOf('.');
    int sep = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'));
    if (dot <= sep + 1 || dot == path.length() - 1) {
      return "";
    }
    return path.substring(dot + 1);
  }

  public static void main(String[] args) {
    String[] names = {
        "folder/s.gr/fg/student-records.pdf",
        "C:\\data.d\\reports\\summary.txt",
        "folder.d/README",
        "folder/.hidden",
        "archive.tar."
    };
    for (String name : names) {
      System.out.println(name + " -> '" + fileExtension(name) + "'");
    }
  }
}

Output:

folder/s.gr/fg/student-records.pdf -> 'pdf'
C:\data.d\reports\summary.txt -> 'txt'
folder.d/README -> ''
folder/.hidden -> ''
archive.tar. -> ''

The dot is compared with sep + 1, not just sep, so a dot in the first position of the final name segment also yields "" — this helper treats .hidden as a hidden file with no extension. FilenameUtils.getExtension() applies the same separator check but makes a different choice there and returns hidden for .hidden; both agree on "" for folder.d/README and archive.tar.. Pick the convention your callers expect.

Choose the right approach

Use FilenameUtils.getExtension() when Apache Commons IO is already on your classpath or when you want a well-tested result with a single call — it handles both separators and dotted directories out of the box. Reach for the String version when you want zero dependencies, cannot add a library, or need a custom rule such as the leading-dot behavior shown above; you then own the separator and trailing-dot checks yourself.

For most projects that already do file I/O, FilenameUtils.getExtension() is the simpler and safer choice. For a small utility or a restricted classpath, the path-aware helper covers the same ground in a few lines.