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"
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;
	}
}

When you run above program, you will get following output:
File extension for Countries.xlsx is xlsx
File extension for Captial.csv is csv
File extension for src/main/resources is 

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

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 Countries.xlsx is xlsx
File extension for Captial.csv is csv
File extension for src/main/resources is 









Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012