我有一个代码
private void processFiles() {
try {
Files.walk(Paths.get(Configurations.SOURCE_PATH))
.filter(new NoDestinationPathFilter()) //<--This one
.filter(new NoMetaFilesOrDirectories()) //<--and this too
.forEach(
path -> {
new FileProcessorFactory().getFileProcessor(
path).process(path);
});
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
截至目前,我有各种其他方法,与上述方法相同,只是在滤波器方面有所不同.有些方法有额外的过滤器,有些方法有不同或没有.
是否有可能创建一个条件所需的过滤器集合并动态传入.并且集合中的所有过滤器都应用于流.我不想对正在应用的过滤器列表进行硬编码.我想让它基于配置.我如何实现这一目标?
你可以使用Files.find():
private void processFiles(final Path baseDir, final Consumer<? super Path> consumer,
final Collection<BiPredicate<Path, BasicFileAttributes>> filters)
throws IOException
{
final BiPredicate<Path, BasicFileAttributes> filter = filters.stream()
.reduce((t, u) -> true, BiPredicate::and);
try (
final Stream<Path> stream = Files.find(baseDir, Integer.MAX_VALUE, filter);
) {
stream.forEach(consumer);
}
}
Run Code Online (Sandbox Code Playgroud)
是的,这意味着转换过滤器......
另见javadoc of BiPredicate和BasicFileAttributes; 特别是,BiPredicate有一种.and()方法,你会发现在你的情况下有用.
| 归档时间: |
|
| 查看次数: |
244 次 |
| 最近记录: |