Wenn ich etwas mit Dateien nur auf der ersten Ebene des Verzeichnisses tun möchten, gibt es einen Unterschied zwischen Files.walkFileTree(...)
oder Files.walk(...)
?Was ist der Unterschied zwischen Files.list und Files.walkFileTree und Files.walk mit maxdepth = 1?
Files.walkFileTree(directory, Collections.emptySet(), 1, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
doSomething(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
// log exc
return FileVisitResult.CONTINUE;
}
});
gegen
Files.list(directory)
.forEach(path -> {
try {
doSomething(path);
} catch (IOException exc) {
// log exc
}
});
gegen
Files.walk(directory, 1)
.forEach(path-> {
try {
doSomething(path);
} catch (IOException exc) {
// log exc
}
});