How to Get the File Extension of a File in Java

  1. Get the File Extension Using the getExtension() Method in Java
  2. Get the File Extension of a File With the lastIndexOf() Method in Java
  3. Get the File Extension of a File With String Parsing in Java
  4. Get the File Extension of a File Using the replaceAll() Method in Java
  5. Get the File Extension of a File Using contains() and lastIndexOf() Method in Java
  6. Get the File Extension of a File Using ternary operators in Java
  7. Get the File Extension of a File Using stream() Method in Java
  8. Get the File Extension of a File Using Regex in Java

This tutorial introduces how to get the file extension of a file in Java and also lists some example codes to understand the topic.

Get the File Extension Using the getExtension() Method in Java

To get an extension of a file, we can use the getExtension() method of the FilenameUtils class. This method returns the extension of the file. Since this method belongs to Apache commons library, you must download the library from Apache official site to use JARs in your project.

import org.apache.commons.io.FilenameUtils;

public class SimpleTesting {
	public static void main(String[] args){
		String fileName = "student-records.pdf";
		String fe = FilenameUtils.getExtension(fileName);
		System.out.println("File extension is : "+fe);
	}
}

Output:

File extension is : pdf

Get the File Extension of a File With the lastIndexOf() Method in Java

If you don’t want to use any built-in method then use the given code example that uses lastIndexOf() method to get the file extension. It is the simplest and easy way that involves only string methods. See the example below.

public class SimpleTesting {
	public static void main(String[] args){
		String fileName = "student-records.pdf";
		String fe = "";
		int i = fileName.lastIndexOf('.');
		if (i > 0) {
		    fe = fileName.substring(i+1);
		}
		System.out.println("File extension is : "+fe);
	}
}

Output:

File extension is : pdf

Get the File Extension of a File With String Parsing in Java

This is another solution that includes several scenarios including the one (if dot(.) is in the file path). This method returns the accurate result even if the file path has a dot(.) in between the file path. See the example below.

public class SimpleTesting {
	public static void main(String[] args){
		String fileName = "folder\s.gr\fg\student-records.pdf";
		String fe = "";
		char ch;
		int len;
		if(fileName==null || (len = fileName.length())==0 || (ch = fileName.charAt(len-1))=='/' || ch=='\\' ||ch=='.' ) {
			fe = "";
		}
		int dotInd = fileName.lastIndexOf('.'),	sepInd = Math.max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\'));
		if( dotInd <= sepInd ) {
			fe = "";
		}
		else {
			fe = fileName.substring(dotInd+1).toLowerCase();
		}
		System.out.println("File extension is : "+fe);
	}
}

Output:

File extension is : pdf

Get the File Extension of a File Using the replaceAll() Method in Java

We can use replaceAll() method to get file extension as we did in the below example. We use regular expression in this method and collect the result into a variable.

public class SimpleTesting {
	public static void main(String[] args){
		String fileName = "folder\s.gr\fg\student-records.pdf";
		String fe = "";
		fe = fileName.replaceAll("^.*\\.(.*)$", "$1");
		System.out.println("File extension is : "+fe);
	}
}

Output:

File extension is : pdf

Get the File Extension of a File Using contains() and lastIndexOf() Method in Java

The contains() method is used to check whether the specified char is present in the string or not and the lastIndexOf() method returns an index value of the specified char which is passed into substring() method to get file extension. We use these methods in this code to get file extension. See the example below.

public class SimpleTesting {
	public static void main(String[] args){
		String fileName = "folder\s.gr\fg\student-records.pdf";
		String fe = "";
		if (fileName.contains("."))
		     fe = fileName.substring(fileName.lastIndexOf(".")+1);
		System.out.println("File extension is : "+fe);
	}
}

Output:

File extension is : pdf

Get the File Extension of a File Using ternary operators in Java

If you are comfortable with ternary operators(?, :) then use it with substring() and lastIndexOf() method. It reduces the line of code and returns the result in a single statement.

public class SimpleTesting {
	public static void main(String[] args){
		String fileName = "folder\s.gr\fg\student-records.pdf";
		String fe = "";
		if (fileName.contains(".")) {
			int i = fileName.lastIndexOf('.');
			fe = i > 0 ? fileName.substring(i + 1) : "";
		}
		System.out.println("File extension is : "+fe);
	}
}

Output:

File extension is : pdf

Get the File Extension of a File Using stream() Method in Java

We can use stream() method of Arrays class to convert the file name into stream and use split() method to break the file name from the dot(.). See the example below.

import java.util.Arrays;
public class SimpleTesting {
	public static void main(String[] args){
		String fileName = "folder\s.gr\fg\student-records.pdf";
		String fe = "";
		if (fileName.contains(".")) {
			fe = Arrays.stream(fileName.split("\\.")).reduce((a,b) -> b).orElse(null);
		}
		System.out.println("File extension is : "+fe);
	}
}

Output:

File extension is : pdf

Get the File Extension of a File Using Regex in Java

This is another solution that uses regex package. The compile() and matcher() method of Pattern class is used to fetch extension of the file in this Java example. See the example below.

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SimpleTesting {
	public static void main(String[] args){
		String fileName = "folder\s.gr\fg\student-records.pdf";
		String fe = "";
		final Pattern PATTERN = Pattern.compile("(.*)\\.(.*)");
		Matcher m = PATTERN.matcher(fileName);
		if (m.find()) {
			fe = m.group(2);
		}
		System.out.println("File extension is : "+fe);
	}
}

Output:

File extension is : pdf

Related Article - Java IO

  • How to Read All Files of a Folder in Java
  • Related Article - Java File

  • How to Get the Current Working Directory in Java
  • How to Create a File and Write Data to It in Java