10.9. Filtering Files

Problem

You need to select all the files in a directory ending in .xml, or you need to select only files (not subdirectories) contained in a directory. In other words, you need to filter a list of files.

Solution

Use one of the many implementations of IOFileFilter in the org.apache.commons.io.filefilter package. This package contains various implementations of FileFilter and FilenameFilter, which can be used to filter the contents of a directory. The following example uses SuffixFileFilter to return an array of filenames that end in .xml:

import java.io.FilenameFilter;
import org.apache.commons.io.filefilter.SuffixFileFilter;
import org.apache.commons.lang.ArrayUtils;

File rootDir = new File(".");
FilenameFilter fileFilter = new SuffixFileFilter(".xml");
String[] xmlFiles = rootDir.list( fileFilter );
System.out.println( "*** XML Files" );
System.out.println( ArrayUtils.toString( xmlFiles ) );

This code searches for all files ending in .xml in the current directory. Running this in the root of the example project matches one file, project.xml, producing the following output:

*** XML Files
{project.xml}

Discussion

The org.apache.commons.io.filefilter package contains a number of implementations of FilenameFilter and FileFilter. PrefixFileFilter and SuffixFileFilter let you match files and directories by a prefix or suffix. NameFileFilter matches a file or a directory to a specific name. DirectoryFileFilter accepts only directories. AndFileFilter, OrFileFilter, and NotFileFilter allow for the logical combination of filters. The following example uses a combination of the file filters in this package to list .htm or .html files in a directory:

import org.apache.commons.io.filefilter.AndFileFilter;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.NotFileFilter;
import org.apache.commons.io.filefilter.OrFileFilter;
import org.apache.commons.io.filefilter.SuffixFileFilter;
import org.apache.commons.lang.ArrayUtils;

IOFileFilter htmlFilter = 
    new OrFileFilter( new SuffixFileFilter("htm"),
                      new SuffixFileFilter("html") );
IOFileFilter notDirectory = new NotFileFilter( DirectoryFileFilter.INSTANCE );
FilenameFilter fileFilter = new AndFileFilter( htmlFilter, notDirectory );

String[] htmlFiles = rootDir.list(fileFilter);
System.out.println( "*** HTML Files" );
System.out.println( ArrayUtils.toString( htmlFiles ) );

This example combines two SuffixFileFilter instances in an OrFileFilter to match .htm or .html files. Wrapping a DirectoryFileFilter with a NotFileFilter creates a filter that will accept files and reject directories. Combining these two filters in an AndFileFilter creates a filter to list files with either suffix. Every filter defined in the org.apache.commons.io.filefilter package is an implementation of the IOFileFilter, which implements both the java.io.FileFilter and java.io.FilenameFilter interfaces.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.98.148