Poly-SO-mix代码与Apache-Commons-FileUtils的比较:iterateFiles和listFiles
apachecommons io在FileUtils中有类似的方法iterateFiles和listFiles,由
Bozho
参数检查有多种方式,但从不使用“System.exit(9)”!
他们在listFiles实现中使用静态的linkedList,这是作者在书中建议的
poly
.
它们重用字段以匹配所有目录:
TrueFileFilter.INSTANCE实例
true filter的单例实例(来自apacheapi,Singleton?)
我不确定它的含义。他们当然可以重用他们的代码。
有一些非常简洁的——我觉得不错——评估点,没有模糊的虚拟变量。
return listFiles(directory, filter,
(recursive ? TrueFileFilter.INSTANCE : FalseFileFilter.INSTANCE));
方法所在的FileUtils类只有4个字段值——每个字段大约有2.5个方法!
现在我为我的班级感到羞愧。
它们使用它们,但显然由于FileUtils类的不同目标,它们允许用户处理它们,列表中没有集中的集合。
没有额外的声明。
-
相似性:linkedList和list
-
不同之处:init更少,dec更少,方法的字段密度更小——简洁
-
不同的目标:所以类的最终用户情况,FileUtils更多的后端
-
区别(自然):在SO中处理异常,而不是在FileUtils中(也许这就是它如此干净的原因)
我喜欢评论,尤其是
source
apachecommons:FileUtils.java、listFiles、iterateFiles-代码片段
/**
* 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();
}
//****剪下一部分******//
/**
* 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();
}