本文整理了Java中org.apache.commons.io.FileUtils.iterateFilesAndDirs()
方法的一些代码示例,展示了FileUtils.iterateFilesAndDirs()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FileUtils.iterateFilesAndDirs()
方法的具体详情如下:
包路径:org.apache.commons.io.FileUtils
类名称:FileUtils
方法名:iterateFilesAndDirs
[英]Allows iteration over the files in given directory (and optionally its subdirectories).
All files found are filtered by an IOFileFilter. This method is based on #listFilesAndDirs(File,IOFileFilter,IOFileFilter), which supports Iterable ('foreach' loop).
The resulting iterator includes the subdirectories themselves.
[中]允许对给定目录(以及可选的子目录)中的文件进行迭代。
找到的所有文件都由IOFileFilter筛选。此方法基于#listFilesAndDirs(File,IOFileFilter,IOFileFilter),它支持Iterable('foreach'循环)。
生成的迭代器包括子目录本身。
代码示例来源:origin: commons-io/commons-io
final Iterator<File> files = FileUtils.iterateFilesAndDirs(subDir1,
new WildcardFileFilter("*.*"),
new WildcardFileFilter("*"));
代码示例来源:origin: Hive2Hive/Hive2Hive
/**
* Visit all files recursively and calculate the hash of the file. Folders are also added to the result.
*
* @param root the root folder
* @return a map where the key is the relative file path to the root and the value is the hash
* @throws IOException if hashing fails
*/
public static Map<String, byte[]> visitFiles(File root) throws IOException {
Map<String, byte[]> digest = new HashMap<String, byte[]>();
Iterator<File> files = FileUtils.iterateFilesAndDirs(root, TrueFileFilter.TRUE, TrueFileFilter.TRUE);
while (files.hasNext()) {
File file = files.next();
if (file.equals(root)) {
// skip root folder
continue;
}
String path = FileUtil.relativize(root, file).toString();
byte[] hash = HashUtil.hash(file);
if (file.isDirectory()) {
digest.put(path + FileUtil.getFileSep(), hash);
} else {
digest.put(path, hash);
}
}
return digest;
}
}
内容来源于网络,如有侵权,请联系作者删除!