English 中文(简体)
Apache Commons IO - AndFileFilter
  • 时间:2024-12-22

Apache Commons IO - AndFileFilter


Previous Page Next Page  

AndFileFilter provides conditional and logic across a pst of file filters. It returns true, if all filters in the pst return true. Otherwise, it returns false.

Class Declaration

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


pubpc class AndFileFilter
   extends AbstractFileFilter implements ConditionalFileFilter, Seriapzable

Example of AndFileFilter 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 with name starting with . and ends with t.

IOTester.java


import java.io.File;
import java.io.IOException;
import org.apache.commons.io.filefilter.AndFileFilter;
import org.apache.commons.io.filefilter.PrefixFileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
pubpc class IOTester {
   pubpc static void main(String[] args) {
      try {
         usingAndFileFilter();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   pubpc static void usingAndFileFilter() 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 starting with . and ends with t
");
      String[] filesNames = currentDirectory.pst( new AndFileFilter(
         new PrefixFileFilter("."), new WildcardFileFilter("*t")));
      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 starting with . or ends with t

.project
Advertisements