While doing file I/O operations in Java you may need to know the extension of the file. In this post we’ll see a Java program to get the file extension. Since there is no direct Java File API method to get the file extension you will have to use the Java String class methods to do that.
Another option is to use Apache IO where FilenameUtils has a method getExtension() to get the file name. But that requires Apache IO jar in the class path.
Getting file extension Java program
1. In String class there is a lastIndexOf(int ch)
method that gives the index of the last occurrence of the
specified character, using that you can get the index of the last '.' and using subString() you can get the string after
that last dot. That way you can get the extension of the passed file.
public class FileExtension { public static void main(String[] args) throws IOException { File file = new File("F:\\knpcode\\links.txt"); String extension = getFileExtension(file); System.out.println("Extension is- " + extension); file = new File("F:\\knpcode\\Ubuntu Page.html"); extension = getFileExtension(file); System.out.println("Extension is- " + extension); } private static String getFileExtension(File file) { // null and file exist check if(file == null || !file.exists()){ return "File not found"; } String fileName = file.getName(); int extIndex = fileName.lastIndexOf("."); // -1 is returned if index is not found if(extIndex == -1) { return ""; }else { return fileName.substring(fileName.lastIndexOf(".")); } } }Output
Extension is- .txt Extension is- .html
If you don’t want the accompanying '.' then use
fileName.substring(fileName.lastIndexOf(".") + 1
2. Using Apache IO library which provides FilenameUtils
class with method getExtension()
which returns the textual part
of the filename after the last dot. If there is no extension then empty string is returned, if file is null then null
is returned.
import java.io.File; import java.io.IOException; import org.apache.commons.io.FilenameUtils; public class FileExtension { public static void main(String[] args) throws IOException { File file = new File("F:\\knpcode\\links.txt"); String extension = getFileExtensionApache(file); System.out.println("Extension is- " + extension); file = new File("F:\\knpcode\\Ubuntu Page.html"); extension = getFileExtensionApache(file); System.out.println("Extension is- " + extension); } private static String getFileExtensionApache(File file) { return FilenameUtils.getExtension(file.getName()); } }Output
Extension is- txt Extension is- html
That's all for the topic Java Program to Find File Extension. If something is missing or you have something to share about the topic please write a comment.
You may also like
- Java Program to Get Files Having a Specific Extension
- Decompress And Untar Multiple Gzipped files in Java
- Convert LocalDate, LocalTime, LocalDateTime to Java Instant
- Cannot Make a Static Reference to The Non-static Method or Field
- Java var Type (Local Variable Type Inference)
- Java Immutable List With Examples
- Advantages and Disadvantages of Autowiring in Spring
- Spring Boot Hello World Application
No comments:
Post a Comment