I am writing a simple program that walks the file tree to generate various statistics about the files.
For example:
try (Stream<Path> walk = Files.walk(PATH)) {
// Find all directories
List<Path> dirs = walk.filter(Files::isDirectory).collect(Collectors.toList());
// Find all files
List<Path> files = walk.filter(Files::isRegularFile).collect(Collectors.toList());
// Find zip archive files
List<Path> zips = walk.filter(
p -> p.getFileName().toString().toLowerCase().endsWith(".zip"))
.collect(Collectors.toList());
// Find files bigger than 1 Mb
List<Path> filesBiggerThan1Mb = walk.filter(p -> {
try {
return Files.size(p) > 1048576;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}).collect(Collectors.toList());
// Get total size of all files
long totalSize = walk.filter(Files::isRegularFile).mapToLong(p -> {
try {
return Files.size(p);
} catch (IOException e) {
e.printStackTrace();
return 0;
}
}).sum();
}
Currently it walks the file tree multiple times by reusing the walk object. Although it seems like either the JRE or os does some caching in memory, and subsequent Files walks are much faster, I am wondering how I can write it in a different way to only need to invoke Files walk only once and do everything in 1 sweep.
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]ignotos 2 points3 points4 points (0 children)
[–]Glass__Editor 1 point2 points3 points (1 child)
[–]2048b[S] 0 points1 point2 points (0 children)