Apache Commons IO - SuffixFileFilter Class



Overview

SuffixFileFilter class filters the files which are based on suffix. This is used in retrieving all the files of a particular type.

Class Declaration

Following is the declaration for org.apache.commons.io.filefilter.SuffixFileFilter Class −

public class SuffixFileFilter 
   extends AbstractFileFilter implements Serializable

Here is the input file we need to parse −

input.txt

Welcome to TutorialsPoint. Simply Easy Learning.

Example - Filtering files with .txt extension.

CommonsIoTester.java

package com.tutorialspoint;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.filefilter.SuffixFileFilter;

public class CommonsIoTester {
   public static void main(String[] args) throws IOException {
      //get the current directory
      File currentDirectory = new File(".");
     
      String[] filesNames = currentDirectory.list( new SuffixFileFilter("txt"));
      for( int i = 0; i < filesNames.length; i++ ) {
         System.out.println(filesNames[i]);
      }
   }
}

Output

It will print the following result −

input.txt
output.txt

Example - Filtering files ending with t.

CommonsIoTester.java

package com.tutorialspoint;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.filefilter.SuffixFileFilter;

public class CommonsIoTester {
   public static void main(String[] args) throws IOException {
      //get the current directory
      File currentDirectory = new File(".");
     
      String[] filesNames = currentDirectory.list( new SuffixFileFilter("t"));
      for( int i = 0; i < filesNames.length; i++ ) {
         System.out.println(filesNames[i]);
      }
   }
}

Output

It will print the following result −

.project
input.txt
output.txt
Advertisements