2

I am try using the following code:

Files.walk(Paths.get("/home"), FileVisitOption.FOLLOW_LINKS)
    .map(p -> p.getFileName())
    .map(f -> f.toString())
    .filter(s -> s.endsWith(".rb"))
    .forEach(o -> System.out.println(o));

When I run it as a regular user, I got AccessDeniedException Wrapped in UncheckIOException..And when Run as root, I got the correct result.

$ java helloJava8.LambdaExpr 
Exception in thread "main" java.io.UncheckedIOException: java.nio.file.AccessDeniedException: /home/lost+found
    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 .......
$sudo java helloJava8.LambdaExpr 
hello.rb

I do know the cause of the problem, but what I want to know how to swallow this exception, because if i wrap the above code in Try-Catch block, I got the method walk finish its work and stop walk-though the fs.

Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
Muhammad Hewedy
  • 29,102
  • 44
  • 127
  • 219

0 Answers0