I'm trying to browse through all the files in the C: \ directory of my computer. Before I was using the IO library, but it was giving some problems and now I'm using NIO, With NIO is working fine, I'm browsing all the files in the directory and its sub-folders, the problem is that there are some directories with access denied, when the code goes through and finds those folder with access denied, it returns an AccessDeniedException exception, up to normal. The problem occurs when I'm going to use a try ... catch to "skip" these exceptions and keep scrolling, but instead it kinda stops scrolling through other folders. Can someone give a light
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
public class PercorrendoArquivosComSubPasta {
public static void main(String args[]) throws IOException {
Path source = Paths.get("C:\");
try {
Files.walkFileTree(source, new MyFileVisitor());
} catch (java.nio.file.AccessDeniedException ex) {
ex.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
class MyFileVisitor extends SimpleFileVisitor<Path> {
public FileVisitResult visitFile(Path path, BasicFileAttributes fileAttributes){
System.out.println("Nome do arquivo:" + path.getFileName()); return FileVisitResult.CONTINUE;
}
public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes fileAttributes){
System.out.println("----------Nome do diretório:" + path + "----------");
return FileVisitResult.CONTINUE;
}
}