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.
Table of Contents [hide]
Using simple String handling program
Create a simple java class named GetExtensionOfFileMain.java
File extension for Captial.csv is csv
File extension for src/main/resources is
As you can see, by using [String’s contains()](https://java2blog.com/java-string-contains-ignorecase/ “String’s contains()”) and [lastIndexOf()](https://java2blog.com/java-string-lastindexof-example/ “lastIndexOf()”), we are able to get extension of file in java.
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 extension of file in java.
When you run above program, you will get foloowing output:
File extension for Captial.csv is csv
File extension for src/main/resources is
That’s all about java get file extension.