How to get extension of file in java

In this post, we will see how to get extension of file in java. As java.io.File does not provide any direct method for getting extension of file. This is used when you want to process file differently on the basis of its extension. I have worked on a project where I required this utility method.
There are two simple way to get it.
  • using simple String handling program
  • using Apache io

Using simple String handling program:

Create a simple java class named “GetExtensionOfFileMain.java”

When you run above program, you will get following output:

Using Apache io:

You need to download Apcahe io jar and put it to the classpath.
After doing above steps, you just need to call FilenameUtils.getExtension to retrieve file name

When you run above program, you will get foloowing output:

 
 
 
 
 
 

Add Comment