Java Program to Find File Extension

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

Related Posts

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

One Comment

  1. What is the Proxy Design Pattern in Java? The Proxy Design Pattern provides a representative for another object in order to control the client’s access to it. The intent is to provide a placeholder for that object to control access to it. The literal meaning of proxy is “the authority” to represent someone else”. Now, that says a lot of things about the design pattern itself. As exemplified in the introductory paragraph, the proxy could interface to anything, it can be an interface to a network connection, an object in memory, a file or any other resource that is expensive to duplicate. The purpose of the proxy in the object oriented proxy design pattern is to act as a wrapper that is called by the client to get access to the intended object.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.