In this tutorial, you will learn how to get the File extension of a File in Java. First, you will use the String methods. Next, you will learn how to use regular expressions. The last technique will show how to use the Commons IO library.
Create a new project
Follow the link below to create a new Maven project in IntelliJ.
Add dependencies
You should add the Commons IO dependency to the project’s classpath so that you can use it in your project. To achieve this, Copy and paste the following code into the file named pom.xml.
<!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.13.0</version> </dependency>
Press CTRL+SHIFT+O to load the Maven changes. This will download and add the Commons IO dependency to the project’s classpath.
Create a text file
Under the root folder of your project, create a file named data.txt.txt. Yes, the file extension doesn’t look right. Despite that, let it remain that way. Your project should be as shown in the following image.

Using the String methods
Make a UsingStringMethods.java file in the src/main/java/com/javawhizz package. Copy and paste the following code into the file.
package com.javawhizz; import java.io.File; public class UsingStringMethods { public static void main(String[] args) { String file = new File("data.txt.txt") .getName(); int dotIndex = file.lastIndexOf("."); System.out.println(dotIndex); if (dotIndex != -1){ String extension = file.substring(dotIndex + 1); System.out.println("The file extension is: "+extension); }else { System.out.println("The file has no extension"); } } }
In this technique, you start by creating an instance of a file using the File
class. Since it expects the path name for the file, pass the path name as the argument.
This will return a File instance for the provided path name. To access the file name, call the getName()
method on the new file object. This will return the name of the file.
Generally, the file name is the name after the dot character. As a result, use the lastIndexOf()
method to get the index of the last symbol that matches the dot character. Note that since the file name is data.txt.txt, it will return the index of the last dot in the String.
Next, use the substring()
method to get the last characters after the dot character. To achieve this, you should increment the index of the dot character by one then pass the result as the argument. With this in place, your program should return the extension of the File.
Output
The file extension is: txt
Using a regular expression
Make a UsingRegularExpressions.java file in the src/main/java/com/javawhizz package. Copy and paste the following code into the file.
package com.javawhizz; import java.io.File; import java.util.regex.Matcher; import java.util.regex.Pattern; public class UsingRegularExpressions { public static void main(String[] args) { Pattern pattern = Pattern.compile("\\.(\\w+)$"); Matcher matcher = pattern.matcher( new File("data.txt.txt") .getName()); if (matcher.find()){ String extension = matcher.group(1); System.out.println("The file extension is: "+extension); }else { System.out.println("The file has no extension"); } } }
To use a regular expression, you must match a pattern against the file name. Start by creating a pattern using the Pattern.compile()
method.
Pass the regular expression as the argument of the static method. As a result, this will compile the regular expression into a pattern that can match the file name.
Next, call the matcher()
method of the pattern object to supply the input to match against. The input in this case is the name of the file. Use the getName()
method of the File
class to provide the name of the file.
To get the file extension, call the group()
method of the Matcher
object then pass 1
as the argument. As a result, this will return the sequence of characters in the group of the previous match.
Output
The file extension is: txt
Using the getExtension() method
Make a UsingGetExtensionMethod.java file in the src/main/java/com/javawhizz package. Copy and paste the following code into the file.
package com.javawhizz; import org.apache.commons.io.FilenameUtils; import java.nio.file.Paths; public class UsingGetExtensionMethod { public static void main(String[] args) { String extension = FilenameUtils.getExtension( String.valueOf(Paths.get("data.txt.txt") .getFileName())); if (! extension.isEmpty()){ System.out.println("The file extension is: "+extension); }else { System.out.println("The file has no extension"); } } }
The Commons IO library usually provides static methods to work with files. To get the file extension in this example, use the getExtension()
method.
You can access this method from the class named FilenameUtils.
Next, pass the name of the file as the argument of the file. To supply the file name, you can leverage the getFileName()
method of Path
class.
If your program runs without any errors, you should see the following output.
Output
The file extension is: txt
Conclusion
In this tutorial, you have learned how to get the file extension in Java. first, you have learned how to use String methods. Next, you have learned how to use a regular expression. In the last step, you have learned how to use the Commons IO library.
Happy Hacking!
0 Comments