Apache Commons IO - PrefixFileFilter Class



Overview

PrefixFileFilter class filters the files which are based on prefix.

Class Declaration

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

public class PrefixFileFilter 
   extends AbstractFileFilter implements Serializable

Here is the input file we need to parse −

input.txt

Welcome to TutorialsPoint. Simply Easy Learning.

Example - Filtering files starting with in.

CommonsIoTester.java

package com.tutorialspoint;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.filefilter.PrefixFileFilter;

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

Output

It will print the following result −

File starting with in
input.txt

Example - Filtering files starting with out.

CommonsIoTester.java

package com.tutorialspoint;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.filefilter.PrefixFileFilter;

public class CommonsIoTester {
   public static void main(String[] args) {
      //get the current directory
      File currentDirectory = new File(".");

      System.out.println("File starting with out");
      String[] filesNames = currentDirectory.list( new PrefixFileFilter("out") );
      for( int i = 0; i < filesNames.length; i++ ) {
         System.out.println(filesNames[i]);
      }
   }
}

Output

It will print the following result −

File starting with out
output.txt
Advertisements