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
}
});
The walkFileTree method from the NIO Files class is a static method that we used to walk a file tree. The walking starts at a given Path. It can either be a file or a directory. The traversal starts at that node and it recursively visits each node in the tree in a depth-first fashion.
list() returns the array of files and directories in the directory defined by this abstract path name. The method returns null, if the abstract pathname does not denote a directory.
A regular file is a sequence of bytes stored permanently in a file system.
Answer and Explanation: 1. The only true statement is C), All methods defined in an interface must be implemented when used by another class.
Files.walk returns a stream that is lazily populated with Path by recursively walking the file tree rooted at a given starting file. The file tree is traversed depth-first.
The difference between walk and walkFileTree is that they supply different interfaces for walking the tree: walkFileTree takes FileVisitor, walk gives Stream<Path>.
Java Files.walk tutorial shows how to walk files in Java with Files.walk . Files.walk returns a stream that is lazily populated with Path by recursively walking the file tree rooted at a given starting file. The file tree is traversed depth-first.
The walkFileTree method from the NIO Files class is a static method that we used to walk a file tree. The walking starts at a given Path. It can either be a file or a directory. The traversal starts at that node and it recursively visits each node in the tree in a depth-first fashion.
Using following code as test, I got the hang of the issue. The main difference between walk*
and list
is that list(dir)
gives a stream of files in the directory dir
, while both walk*
method walk the subtree of its argument including the root of subtree—the directory itself.
The difference between walk
and walkFileTree
is that they supply different interfaces for walking the tree: walkFileTree
takes FileVisitor
, walk
gives Stream<Path>
.
public class FilesTest {
public static void main(String[] args) {
final String pwd = System.getProperty("user.dir");
System.out.println("Working Directory = " + pwd);
Path dir = Paths.get(pwd);
System.out.println("Files.walk");
try {
Files.walk(dir, 1).forEach(path -> FilesTest.doSomething("walk", path));
} catch (IOException e) {
logException("walk", e);
}
System.out.println("Files.walkFileTree");
try {
Files.walkFileTree(dir, Collections.emptySet(), 1, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
doSomething("visitFile", file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
logException("visitFile", exc);
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
logException("walkFileTree", e);
}
System.out.println("Files.list");
try {
Files.list(dir).forEach(path -> FilesTest.doSomething("dir", path));
} catch (IOException e) {
logException("dir", e);
}
}
private static void logException(String title, IOException e) {
System.err.println(title + "\terror: " + e);
}
private static void doSomething(String title, Path file) {
System.out.println(title + "\t: " + file);
}
}
All these 3 solutions look correct, but it's better to use the simplest and the most readable way, so Files.list()
looks natural to solve this problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With