Get File Extension in Java

Exercise:

Write a Java Program to get the file extensions.

1.Get the file extension

Click Here to View the Solution!
import java.io.File;

class Main {

    public static void main(String[] args) {
        File file = new File("Test.java");

        // convert the file name into string
        String fileName = file.toString();
        int index = fileName.lastIndexOf('.');
        if(index > 0) {
            String extension = fileName.substring(index + 1);
            System.out.println("File extension is " + extension);
        }
    }
}
Click Here to View the Output!
file.toString() - Converts the File object into a string.
fileName.lastIndexOf('.') - Returns the last occurrence of character. Since all file extension starts with '.', we use the character '.'.
fileName.substring() - Returns the string after character '.'.
Click Here to View the Explanation!
  • This program is used to find the extension of a file using .toString() method in Java.
  • Initially, in a class Main, a file object is created that will take Test.java as a parameter.
  • The filename is first converted into a string using filename.toString() method.
  • A filenanme.lastIndexOf() method is used that will locate the final occurrence of of ‘.’ in the filename i.e 4.
  • An if statement will check (index > 0), if true, then it will store the string after the index i.e. (index + 1) in the string variable extension using filename.substring() method.
  • Finally, the string in extension will be printed.

2. Get the file extension of all files present in a directory

Click Here to View the Solution!
import java.io.File;

class Main {
    public static void main(String[] args) {
        File directory = new File("Directory");

        // list all files present in the directory
        File[] files = directory.listFiles();
        System.out.println("Files\t\t\tExtension");
        for(File file : files) {

            // convert the file name into string
            String fileName = file.toString();
            int index = fileName.lastIndexOf('.');
            if(index > 0) {
                String extension = fileName.substring(index + 1);
                System.out.println(fileName + "\t" + extension);
            }
        }
    }
}
Click Here to View the Output!
Files                   Extension 
Directory\file1.txt     txt 
Directory\file2.svg     svg 
Directory\file3.java    java 
Directory\file4.py      py 
Directory\file5.html    html
Click Here to View the Explanation!
  • This program is used to find and print the extension of all the files stored in a directory.
  • Initially, a File object is created holding a directory as its parameter.
  • All the files in a directory are stored in an array File using the directory.listFiles() method.
  • A for each loop is used to iterate between the files stored in the array and converting all the filenames into string using file.toString() method.
  • The index variable is used to store the last occurrence of the . in each filename and .store the string after . in the extension variable.
  • Finally, both the filenames and their corresponding extensions are printed. This output is dependent on the directory and the files stored in the directory.
  • Another option is of using .getFileExtension() method of the Gauva Library for printing file extension.
  • Also, a class FilenameUtils of the package Apache Commons IO provides a .getExtension() method for printing the extensions of the files.