Problem with Accessdeniedexception

Asked

Viewed 68 times

0

I am trying to go 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 the NIO, With the NIO is going well, I’m going through all the files of the directory and its sub-folders, the problem is that there are some directories with access denied, When the code goes through and finds these folders with access denied, it returns an Accessdeniedexception exception, until normal. The problem occurs when I’m going to use a Try...catch to "skip" these exceptions and continue traversing, but instead it sort of stops going through other folders. Someone can 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;
     }
  }

1 answer

0

I figure out the solution, just implement the visitFileFailed method in the Myfilevisitor class. Here the solution

Browser other questions tagged

You are not signed in. Login or sign up in order to post.