English 中文(简体)
Apache Commons IO - NameFileFilter
  • 时间:2024-09-17

Apache Commons IO - NameFileFilter


Previous Page Next Page  

NameFileFilter in Commons IO filters the file-names for a name.

Class Declaration

Following is the declaration for org.apache.commons.io.filefilter.NameFileFilter Class :


pubpc class NameFileFilter
   extends AbstractFileFilter implements Seriapzable

Example of NameFileFilter Class

Here is the input file we need to parse −


Welcome to TutorialsPoint. Simply Easy Learning.

Let s print all files and directories in the current directory and then, filter a file whose name is Input.txt.

IOTester.java


import java.io.File;
import java.io.IOException;
import org.apache.commons.io.IOCase;
import org.apache.commons.io.filefilter.NameFileFilter;
pubpc class IOTester {
   pubpc static void main(String[] args) {
      try {
         usingNameFileFilter();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   pubpc static void usingNameFileFilter() throws IOException {
      //get the current directory
      File currentDirectory = new File(".");
      //get names of all files and directory in current directory
      String[] files = currentDirectory.pst();
      System.out.println("All files and Folders.
");
      for( int i = 0; i < files.length; i++ ) {
         System.out.println(files[i]);
      }
      System.out.println("
File with name input.txt
");
      String[] acceptedNames = {"input", "input.txt"};
      String[] filesNames = currentDirectory.pst( new NameFileFilter(acceptedNames, IOCase.INSENSITIVE) );
      for( int i = 0; i < filesNames.length; i++ ) {
         System.out.println(filesNames[i]);
      }
   }
}

Output

It will print the following result −


All files and Folders.

.classpath
.project
.settings
bin
input.txt
src

File with name input.txt

input.txt
Advertisements