If I want to do something with files only on the first level of the directory,
is there a difference between using Files.list(...)
or Files.walkFileTree(...)
or Files.walk(...)
?
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;
}
});
versus
Files.list(directory)
.forEach(path -> {
try {
doSomething(path);
} catch (IOException exc) {
// log exc
}
});
versus
Files.walk(directory, 1)
.forEach(path -> {
try {
doSomething(path);
} catch (IOException exc) {
// log exc
}
});