Exception in thread "main" java.nio.file.AccessDeniedException: /tmp/pulse-2L9K88eMlGn7
with Files.walkFileTree when you have called that method wanting it to work recursively, you might find there are two reason for this happening:
a. there's a directory along the walk to which you have no read access
b. you have not implemented FileVisitor.visitFileFailed
If both a. and b. are the case, what happens is that the aforementioned Exception is thrown and the walk is terminated.
The solution is to ensure that you implement FileVisitor.visitFileFailed. Even when there is an unreadable directory on the walk, it can continue unless explicitly prevented from doing so. The following code shows how to recurse a Path without it terminating in the presence of an unreadable directory and it can be downloaded HERE
import java.io.File;
import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
public class Recurse {
public static void main(String[] args) throws IOException {
Path start = new File(args[0]).toPath();
Files.walkFileTree(start,
new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
System.out.printf("Visiting file %s\n", file);
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;
}
});
}
}
import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
public class Recurse {
public static void main(String[] args) throws IOException {
Path start = new File(args[0]).toPath();
Files.walkFileTree(start,
new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
System.out.printf("Visiting file %s\n", file);
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;
}
});
}
}