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
Using simple String handling program
Create a simple java class named GetExtensionOfFileMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package org.arpit.java2blog; import java.io.File; public class GetExtensionOfFileMain { public static void main(String[] args) { ClassLoader classLoader = GetExtensionOfFileMain.class.getClassLoader(); File xlsxFile=new File(classLoader.getResource("Countries.xlsx").getFile()); String fileExtension=getExtensionOfFile(xlsxFile); System.out.println("File extension for Countries.xlsx is "+ fileExtension); File csvFile=new File(classLoader.getResource("Capital.csv").getFile());; String fileExtensionCSV=getExtensionOfFile(csvFile); System.out.println("File extension for Captial.csv is "+fileExtensionCSV); File folder=new File("src/main/resources"); String fileExtensionFolder=getExtensionOfFile(folder); System.out.println("File extension for src/main/resources is "+fileExtensionFolder); } public static String getExtensionOfFile(File file) { String fileExtension=""; // Get file Name first String fileName=file.getName(); // If fileName do not contain "." or starts with "." then it is not a valid file if(fileName.contains(".") && fileName.lastIndexOf(".")!= 0) { fileExtension=fileName.substring(fileName.lastIndexOf(".")+1); } return fileExtension; } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package org.arpit.java2blog; import java.io.File; import org.apache.commons.io.FilenameUtils; public class GetFileExtensionApache { public static void main(String[] args) { ClassLoader classLoader = GetFileExtensionApache.class.getClassLoader(); File xlsxFile=new File(classLoader.getResource("Countries.xlsx").getFile()); String fileExtension=FilenameUtils.getExtension(xlsxFile.getName()); System.out.println("File extension for Countries.xlsx is "+ fileExtension); File csvFile=new File(classLoader.getResource("Capital.csv").getFile());; String fileExtensionCSV=FilenameUtils.getExtension(csvFile.getName()); System.out.println("File extension for Captial.csv is "+fileExtensionCSV); File folder=new File("src/main/resources"); String fileExtensionFolder=FilenameUtils.getExtension(folder.getName()); System.out.println("File extension for src/main/resources is "+fileExtensionFolder); } } |
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.