FilenameUtils.getExtension() Example

FilenameUtils.getExtension() Example | The getExtension() method of FilenameUtils class given in Apache Commons IO API returns the extension of the given file. It returns the textual part of the fileName after the last dot (‘.’) character. There must be no directory separator after the dot. 

Method syntax:- public static String getExtension(final String fileName) throws IllegalArgumentException

To demonstrate the FilenameUtils.getExtension() method we have to include the Apache Commons IO library. Maven dependency for Apache Commons IO:-

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>

We can get the latest versions of the Apache Commons IO library from Maven Central. Let us see an example of FilenameUtils.getExtension() method.

import java.io.File;

import org.apache.commons.io.FilenameUtils;

public class Main {
   public static void main(String[] args) {
      File file = new File("test.txt");
      String fileName = file.toString();
      String extension = FilenameUtils.getExtension(fileName);
      System.out.println("Extension: " + extension);
   }
}

Output:-

Extension: txt

The FilenameUtils.getExtension() method is defined as follows:-

public static String getExtension(final String fileName) 
          throws IllegalArgumentException {
   if (fileName == null) {
      return null;
   }
   final int index = indexOfExtension(fileName);
   if (index == NOT_FOUND) {
      return EMPTY_STRING;
   }
   return fileName.substring(index + 1);
}

In get File extension in Java, we have discussed different ways to get the extension of a file in Java. Similar to other ways, FilenameUtils.getExtension() method also internally uses lastIndexOf() and substring() method of Java String class.

The FilenameUtils.getExtension() method throws IllegalArgumentException in Windows operating systems if the fileName parameter is, in fact, the identifier of an Alternate Data Stream, for example, “foo.exe:bar.txt”. This method throws IllegalArgumentException only in Windows.

Let us see some more examples of FilenameUtils.getExtension() method using different files:-

import java.io.File;

import org.apache.commons.io.FilenameUtils;

public class Main {
   public static void main(String[] args) {
      File file1 = new File("/home/user/Downloads/commons-io-2.11.0.jar");
      System.out.println("Extension-1: " + 
         FilenameUtils.getExtension(file1.toString()));

      File file2 = new File("foo.exe:bar.txt");
      System.out.println("Extension-2: " + 
         FilenameUtils.getExtension(file2.toString()));

      File file3 = new File("apt-helper");
      System.out.println("Extension-3: " + 
         FilenameUtils.getExtension(file3.toString()));
   }
}

Output:-

Extension-1: jar
Extension-2: txt
Extension-3: 

When fileName is empty string then FilenameUtils.getExtension() method also returns an empty string. And when we pass null as file name then FilenameUtils.getExtension() method returns null.

import org.apache.commons.io.FilenameUtils;

public class Main {
   public static void main(String[] args) {
      String fileName = null;
      System.out.println("Extension-1: " +
         FilenameUtils.getExtension(fileName));

      fileName = "";
      System.out.println("Extension-2: " + 
         FilenameUtils.getExtension(fileName));
   }
}

Output:- 

Extension-1: null
Extension-2: 

Sometimes a single dot (‘.’) character is added to the file name to make it a hidden file. Here are some examples of them:-

import java.io.File;

import org.apache.commons.io.FilenameUtils;

public class Main {
   public static void main(String[] args) {
      File file1 = new File(".gitconfig");
      System.out.println("Extension-1: " +
         FilenameUtils.getExtension(file1.toString()));

      File file2 = new File(".mongorc.js");
      System.out.println("Extension-2: " +
         FilenameUtils.getExtension(file2.toString()));
   }
}

Output:-

Extension-1: gitconfig
Extension-2: js

Get notes to make your learning process easy. These are specially designed for beginners who want to learn coding through simple words, programs, and examples. You can use it as your reference and for revision purposes.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.