我正在尝试将以下Java8片段转换为Java7:
Files.walk(Paths.get(modifiedPathName))
.filter(Files::isRegularFile)
.forEach(path -> {
mutatedFileStorage.add((T) application.readFromFile(path));
});
我尝试了以下内容:
Iterator<Path> it = Files.walkFileTree(Paths.get(modifiedPathName)).iterator();
Path path = null;
while(it.hasNext()) {
path = it.next();
if(Files.isRegularFile(path)) {
mutatedFileStorage.add((T) application.readFromFile(path));
}
}
问题是,我一直收到错误:
error: no suitable method found for walkFileTree(Path)
Iterator<Path> it = Files.walkFileTree(Paths.get(modifiedPathName)).iterator();
我不知道如何将Files.walk
转换为Java7,这是我最好的尝试?我可以对我的Java7代码进行一些小修改来修复它吗?或者我是否必须完全做其他事情?
答案 0 :(得分:3)
我认为你想要使用Files.walkFileTree
:
final Collection<T> finalMutatedFileStorage = mutatedFileStorage;
final Application finalApplication = application; // guessing at the type here
Files.walkFileTree(Paths.get(modifiedPathName), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
if (attrs.isRegularFile()) {
finalMutatedFileStorage.add((T) finalApplication.readFromFile(file));
}
return FileVisitResult.CONTINUE;
}
});