Avoid Java 8 Files.walk(..) termination cause of ( java.nio.file.AccessDeniedException ) [duplicate]
I am using Java 8 Files.walk(..) to count .mp3
files contained inside a folder and all the folders inside it. In other words, I am visiting all the levels of the file tree.
When I get java.nio.file.AccessDeniedException
the Stream
closes, I don't want this behavior. I need it to ignore or print the exception and continue counting files. Below is the code I use :) :
/**
* Count files in a directory (including files in all sub
* directories)
*
* @param directory
* the directory to start in
* @return the total number of files
*/
public int countFiles(File dir) {
if (dir.exists())
try (Stream<Path> paths = Files.walk(Paths.get(dir.getPath()), FileVisitOption.FOLLOW_LINKS)) {
return (int) paths.filter(path -> {
// i am using something different here but i changed
// it just for the purpose of StackOverFlow question
return path.toString().contains(".mp3");
}).count();
} catch (IOException ex) {
//Main.logger.log(Level.WARNING, "", ex);
ex.printStackTrace();
}
return 0;
}
StackTrace of error:
java.io.UncheckedIOException: java.nio.file.AccessDeniedException: C:\$Recycle.B
in\S-1-5-18
at java.nio.file.FileTreeIterator.fetchNextIfNeeded(FileTreeIterator.java:88)
at java.nio.file.FileTreeIterator.hasNext(FileTreeIterator.java:104)
at java.util.Iterator.forEachRemaining(Iterator.java:115)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.jav
a:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.LongPipeline.reduce(LongPipeline.java:438)
at java.util.stream.LongPipeline.sum(LongPipeline.java:396)
at java.util.stream.ReferencePipeline.count(ReferencePipeline.java:526)
at smartcontroller.SmartController$InputService$1.countFiles(SmartController.ja
va:2092)
...
Similar question , though not the same I need a Stream to be returned .
Working around access denied in a FileWalking Tree in Java7 .
Answer
Here is a temporary solution , which can be improved to use Java 8 Streams and Lambdas.
int[] count = {0};
try {
Files.walkFileTree(
Paths.get(dir.getPath()),
new HashSet<FileVisitOption>(Arrays.asList(FileVisitOption.FOLLOW_LINKS)),
Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
System.out.printf("Visiting file %s\n", file);
++count[0];
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException e)
throws IOException {
System.err.printf("Visiting failed for %s\n", file);
return FileVisitResult.SKIP_SUBTREE;
}
@Override
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs)
throws IOException {
System.out.printf("About to visit directory %s\n", dir);
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
// handle exception
}