Provides conditional And logic across a list of file filters. Returns true if all filters in the list return true. Otherwise, it returns false.

Class Declaration

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

  1. public class AndFileFilter
  2. extends AbstractFileFilter implements ConditionalFileFilter, Serializable

Example of AndFileFilter Class

Here is the input file we need to parse −

  1. 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

  1. import java.io.File;
  2. import java.io.IOException;
  3.  
  4. import org.apache.commons.io.filefilter.AndFileFilter;
  5. import org.apache.commons.io.filefilter.PrefixFileFilter;
  6. import org.apache.commons.io.filefilter.WildcardFileFilter;
  7.  
  8. public class IOTester {
  9. public static void main(String[] args) {
  10. try {
  11. usingAndFileFilter();
  12. } catch(IOException e) {
  13. System.out.println(e.getMessage());
  14. }
  15. }
  16.  
  17. public static void usingAndFileFilter() throws IOException {
  18. //get the current directory
  19. File currentDirectory = new File(".");
  20.  
  21. //get names of all files and directory in current directory
  22. String[] files = currentDirectory.list();
  23. System.out.println("All files and Folders.n");
  24. for( int i = 0; i < files.length; i++ ) {
  25. System.out.println(files[i]);
  26. }
  27.  
  28. System.out.println("nFile starting with . and ends with tn");
  29. String[] filesNames = currentDirectory.list(
  30. new AndFileFilter(new PrefixFileFilter("."), new WildcardFileFilter("*t")));
  31. for( int i = 0; i < filesNames.length; i++ ) {
  32. System.out.println(filesNames[i]);
  33. }
  34. }
  35. }

Output

It will print the following result.

  1. All files and Folders.
  2.  
  3. .classpath
  4. .project
  5. .settings
  6. bin
  7. input.txt
  8. src
  9.  
  10. File starting with . or ends with t
  11.  
  12. .project