This example shows how to get file extension from the file name in Java using lastIndexOf and substring methods as well as using the apache commons library.
How to get a file extension in Java?
In most cases, the file name consists of two parts, the name of the file and an extension. There are a couple of ways in which extension can be extracted from the file name or file path.
1) Using the Apache Commons IO library
The easiest way to get the extension of a file is to use the FileNameUtils class of the Apache Commons IO library. The FileNameUtils class has the getExtension
static method that returns the extension from the file name string.
1 |
public static String getExtension(String filename) |
This method returns the extension (usually the text after a dot) or an empty string if there is none.
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 |
package com.javacodeexamples.ioexamples; import org.apache.commons.io.FilenameUtils; public class GetFileExtensionExample { public static void main(String[] args){ String[] strFileNames = { "GetFileExtensionExample.java", "C:/java/GetFileExtensionExample.java", "/etc/init.d/oracleasm", "/usr/local/file", "examples.tar.gz", "/usr/public_html/.htaccess" }; for(String strFileName : strFileNames){ String strExtension = FilenameUtils.getExtension(strFileName); System.out.println("Extension is: " + strExtension); } } } |
Output
1 2 3 4 5 6 |
Extension is: java Extension is: java Extension is: Extension is: Extension is: gz Extension is: htaccess |
The getExtension
method also works with the Unix style directory names where the directory name can also have a dot (.) like below.
1 |
/etc/init.d/oracleasm |
2) Using the lastIndexOf and substring methods of the String class
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 |
package com.javacodeexamples.ioexamples; public class GetFileExtensionExample { public static void main(String[] args){ String[] strFileNames = { "GetFileExtensionExample.java", "C:/java/GetFileExtensionExample.java", "/etc/init.d/oracleasm", "/etc/init.d/listener.ora", "/usr/local/file", "examples.tar.gz", "/usr/public_html/.htaccess" }; for(String strFileName : strFileNames){ String strFileExtension = ""; int intLastDotPosition = strFileName.lastIndexOf("."); int intLastSlashPosition = strFileName.lastIndexOf("/"); if( intLastDotPosition > intLastSlashPosition ){ strFileExtension = strFileName.substring(intLastDotPosition + 1); } System.out.println("File extension is: " + strFileExtension); } } } |
Output
1 2 3 4 5 6 7 |
Extension is: java Extension is: java Extension is: Extension is: ora Extension is: Extension is: gz Extension is: htaccess |
Basically, we start with the extension as an empty string. Then we take the last position of a “.” (dot) and the last position of the “/”. If the last dot comes after the last “/”, we take a substring after the last “.” which gives us the extension. If there is no “.” after last “/”, the extension remains empty (for example in the case of “/etc/init.d/oracleasm” or “/usr/local/file”).
Note: if you are working with a Windows operating system, change this line
1 |
int intLastSlashPosition = strFileName.lastIndexOf("/"); |
to
1 |
int intLastSlashPosition = strFileName.lastIndexOf("\\"); |
For a portable code across different operating systems, change it to
1 |
int intSlashPosition = strFileName.lastIndexOf( System.getProperty("file.separator") ); |
This example is a part of the Java File class tutorial with examples.
Please let me know your views in the comments section below.