Package org.apache.commons.io.filefilter

This package defines an interface (IOFileFilter) that combines both FileFilter and FilenameFilter.

See:
          Description

Interface Summary
IOFileFilter An interface which brings the FileFilter and FilenameFilter interfaces together.
 

Class Summary
AbstractFileFilter An abstract class which implements the Java FileFilter and FilenameFilter interfaces via the IOFileFilter interface.
AndFileFilter This filter produces a logical AND of the two filters specified.
DelegateFileFilter This class turns a Java FileFilter or FilenameFilter into an IO FileFilter.
DirectoryFileFilter This filter accepts Files that are directories.
FalseFileFilter A file filter that always returns false.
FileFilterUtils Useful utilities for working with file filters.
NameFileFilter Filters filenames for a certain name.
NotFileFilter This filter produces a logical NOT of the filters specified.
OrFileFilter This filter produces a logical OR of the two filters specified.
PrefixFileFilter Filters filenames for a certain prefix.
SuffixFileFilter Filters files based on the suffix (what the filename ends with).
TrueFileFilter A file filter that always returns true.
 

Package org.apache.commons.io.filefilter Description

This package defines an interface (IOFileFilter) that combines both FileFilter and FilenameFilter. Besides that the package offers a series of ready-to-use implementations of the IOFileFilter interface including implementation that allow you to combine other such filters.

These filter can be used to list files or in FileDialog, for example.

There are four "primitive" FilenameFilters:

DirectoryFilter Only accept directories
PrefixFileFilter Filter based on a prefix
SuffixFileFilter Filter based on a suffix
NameFileFilter Filter based on a filename

And there are five "boolean" FilenameFilters:

TrueFileFilter Accept all files
FalseFileFilter Accept no files
NotFileFilter Applies a logical NOT to an existing filter
AndFileFilter Combines two filters using a logical AND
OrFileFilter Combines two filter using a logical OR

These boolean FilenameFilters can be nested, to allow arbitrary expressions. For example, here is how one could print all non-directory files in the current directory, starting with "A", and ending in ".java" or ".class":

  File dir = new File(".");
  String[] files = dir.list( 
    new AndFileFilter(
      new AndFileFilter(
        new PrefixFileFilter("A"),
        new OrFileFilter(
          new SuffixFileFilter(".class"),
          new SuffixFileFilter(".java")
        )
      ),
      new NotdFileFilter(
        new DirectoryFileFilter()
      )
    )
  );
  for ( int i=0; i<files.length; i++ ) {
    System.out.println(files[i]);
  }

This package also contains a utility class: FileFilterUtils. It allows you to use all file filters without having to put the in the import section. Here's how the above example will look using FileFilterUtils:

  File dir = new File(".");
  String[] files = dir.list( 
    FileFilterUtils.andFileFilter(
      FileFilterUtils.andFileFilter(
        FileFilterUtils.prefixFileFilter("A"),
        FileFilterUtils.orFileFilter(
          FileFilterUtils.suffixFileFilter(".class"),
          FileFilterUtils.suffixFileFilter(".java")
        )
      ),
      FileFilterUtils.notFileFilter(
        FileFilterUtils.directoryFileFilter()
      )
    )
  );
  for ( int i=0; i<files.length; i++ ) {
    System.out.println(files[i]);
  }

There are a few other goodies in that class so please have a look at the documentation in detail.



Copyright © 2002-2005 The Apache Software Foundation. All Rights Reserved.