How to filter the files by file extensions and show the file names in Java?


The class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories.

To get the list of all the existing files in a directory this class provides five different methods to get the details of all files in a particular folder −

  • String[] list()
  • File[] listFiles()
  • String[] list(FilenameFilter filter)
  • File[] listFiles(FilenameFilter filter)
  • File[] listFiles(FileFilter filter)

Among these, the String[] list(FilenameFilter filter) method returns a String array containing the names of all the files and directories in the path represented by the current (File) object. But the retuned array contains the filenames which are filtered based on the specified filter.

The FilenameFilter is an interface in Java with a single method.

accept(File dir, String name)

To get the file names based on extensions implement this interface as such and pass its object to the above specified list() method of the file class.

Example

Assume we have a folder named ExampleDirectory in the directory D with 7 files and 2 directories as −

The following Java program prints the names of the text files and jpeg files in the path D:\ExampleDirectory separately.

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
public class ListOfFiles {
   public static void main(String args[]) throws IOException {
      //Creating a File object for directory
      File directoryPath = new File("D:\ExampleDirectory");
      FilenameFilter textFilefilter = new FilenameFilter(){
         public boolean accept(File dir, String name) {
            String lowercaseName = name.toLowerCase();
            if (lowercaseName.endsWith(".txt")) {
               return true;
            } else {
               return false;
            }
         }
      };
      FilenameFilter jpgFilefilter = new FilenameFilter(){
         public boolean accept(File dir, String name) {
            String lowercaseName = name.toLowerCase();
            if (lowercaseName.endsWith(".jpg")) {
               return true;
            } else {
               return false;
            }
         }
      };
      //List of all the text files
      String textFilesList[] = directoryPath.list(textFilefilter);
      System.out.println("List of the text files in the specified directory:");
      for(String fileName : textFilesList) {
         System.out.println(fileName);
      }
      String imageFilesList[] = directoryPath.list(jpgFilefilter);
      System.out.println("List of the jpeg files in the specified directory:");
      for(String fileName : imageFilesList) {
         System.out.println(fileName);
      }
   }
}

Output

List of the text files in the specified directory:
SampleFile1.txt
SampleFile2.txt
SapmleFile3.txt
List of the jpeg files in the specified directory:
cassandra_logo.jpg
cat.jpg
coffeescript_logo.jpg
javafx_logo.jpg

Updated on: 02-Aug-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements