Comparison between the Poly-SO mix code and Apache Commons FileUtils: iterateFiles and listFiles
Apache commons-io has similar methods iterateFiles and listFiles in FileUtils, suggested by Bozho.
Parameter checks are done in a variety of ways, but never "System.exit(9)"!
They comp par to null, check its existence with File-type (available method for it),
They use static, linkedList in the listFiles-implementation -- suggested in the book by poly.
They reuse the field to match all dirs:
TrueFileFilter.INSTANCE
Singleton instance of true filter (From Apache API, singleton?)
The two methods are the only methods that use IOFileFilter as a parameter.
I am uncertain of its implications. They can surely reuse their code.
There are some very succinct -- I think nice -- evaluation points, no blurring dummy vars.
Please, see the evalution of (a?b:c), saves stupid dums and if-clauses.
return listFiles(directory, filter,
(recursive ? TrueFileFilter.INSTANCE : FalseFileFilter.INSTANCE));
The FileUtils-class in which the methods resides have only 4 field value -- about 2.5 methods per field!
Now I am shamed with my class.
A striking difference is the use of Exceptions.
They use them but -- apparently due to the different goal of FileUtils class -- they let for user to handle them, no centralised collection in list.
No extra declarations.
Summary
- Similarity: linkedList and list
- Differences: less inits, less decs, smaller density of fields to methods -- succincy
- Different Goals: SO's class end-user case, FileUtils more backend
- Difference(natural): exceptions handling in SO but not in FileUtils (perhaps that is the reason it is so clean)
I liked comments and particularly the source -- much better for educational purposes than reading trivial APIs, I feel. Hope you too :)
Apache Commons: FileUtils.java, listFiles, iterateFiles - code pieces
/**
* Finds files within a given directory (and optionally its
* subdirectories). All files found are filtered by an IOFileFilter.
* <p>
* If your search should recurse into subdirectories you can pass in
* an IOFileFilter for directories. You don't need to bind a
* DirectoryFileFilter (via logical AND) to this filter. This method does
* that for you.
* <p>
* An example: If you want to search through all directories called
* "temp" you pass in <code>FileFilterUtils.NameFileFilter("temp")</code>
* <p>
* Another common usage of this method is find files in a directory
* tree but ignoring the directories generated CVS. You can simply pass
* in <code>FileFilterUtils.makeCVSAware(null)</code>.
*
* @param directory the directory to search in
* @param fileFilter filter to apply when finding files.
* @param dirFilter optional filter to apply when finding subdirectories.
* If this parameter is <code>null</code>, subdirectories will not be included in the
* search. Use TrueFileFilter.INSTANCE to match all directories.
* @return an collection of java.io.File with the matching files
* @see org.apache.commons.io.filefilter.FileFilterUtils
* @see org.apache.commons.io.filefilter.NameFileFilter
*/
public static Collection listFiles(
File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) {
if (!directory.isDirectory()) {
throw new IllegalArgumentException(
"Parameter 'directory' is not a directory");
}
if (fileFilter == null) {
throw new NullPointerException("Parameter 'fileFilter' is null");
}
//Setup effective file filter
IOFileFilter effFileFilter = FileFilterUtils.andFileFilter(fileFilter,
FileFilterUtils.notFileFilter(DirectoryFileFilter.INSTANCE));
//Setup effective directory filter
IOFileFilter effDirFilter;
if (dirFilter == null) {
effDirFilter = FalseFileFilter.INSTANCE;
} else {
effDirFilter = FileFilterUtils.andFileFilter(dirFilter,
DirectoryFileFilter.INSTANCE);
}
//Find files
Collection files = new java.util.LinkedList();
innerListFiles(files, directory,
FileFilterUtils.orFileFilter(effFileFilter, effDirFilter));
return files;
}
/**
* Allows iteration over the files in given directory (and optionally
* its subdirectories).
* <p>
* All files found are filtered by an IOFileFilter. This method is
* based on {@link #listFiles(File, IOFileFilter, IOFileFilter)}.
*
* @param directory the directory to search in
* @param fileFilter filter to apply when finding files.
* @param dirFilter optional filter to apply when finding subdirectories.
* If this parameter is <code>null</code>, subdirectories will not be included in the
* search. Use TrueFileFilter.INSTANCE to match all directories.
* @return an iterator of java.io.File for the matching files
* @see org.apache.commons.io.filefilter.FileFilterUtils
* @see org.apache.commons.io.filefilter.NameFileFilter
* @since Commons IO 1.2
*/
public static Iterator iterateFiles(
File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) {
return listFiles(directory, fileFilter, dirFilter).iterator();
}
//*Cut out a part***//
/**
* Finds files within a given directory (and optionally its subdirectories)
* which match an array of extensions.
*
* @param directory the directory to search in
* @param extensions an array of extensions, ex. {"java","xml"}. If this
* parameter is <code>null</code>, all files are returned.
* @param recursive if true all subdirectories are searched as well
* @return an collection of java.io.File with the matching files
*/
public static Collection listFiles(
File directory, String[] extensions, boolean recursive) {
IOFileFilter filter;
if (extensions == null) {
filter = TrueFileFilter.INSTANCE;
} else {
String[] suffixes = toSuffixes(extensions);
filter = new SuffixFileFilter(suffixes);
}
return listFiles(directory, filter,
(recursive ? TrueFileFilter.INSTANCE : FalseFileFilter.INSTANCE));
}
/**
* Allows iteration over the files in a given directory (and optionally
* its subdirectories) which match an array of extensions. This method
* is based on {@link #listFiles(File, String[], boolean)}.
*
* @param directory the directory to search in
* @param extensions an array of extensions, ex. {"java","xml"}. If this
* parameter is <code>null</code>, all files are returned.
* @param recursive if true all subdirectories are searched as well
* @return an iterator of java.io.File with the matching files
* @since Commons IO 1.2
*/
public static Iterator iterateFiles(
File directory, String[] extensions, boolean recursive) {
return listFiles(directory, extensions, recursive).iterator();
}