I’m looping through a directory in Java, trying to read each of the files. The directory contains some svn files in it which deny access. I could test the file name and skip svn files, but this solution won’t be robust if I find other files with denied access. I tried this:
for(File f : dir.listFiles()){
if(f.canRead()){
System.out.println("Trying " + f.getAbsolutePath());
try{
Scanner sc = new Scanner(f);
}
catch(IOException e){
e.printStackTrace();
}
continue;
}
}
When I get to the .svn file, however, it doesn’t skip; I get this printout:
Trying C:\dir\.svn
java.io.FileNotFoundException: dir\.svn (Access is denied)
canRead(),canWrite(), and canExecute() all have the same problem.
Is there something besides canXXX() that I can use to skip denied access files?
.svnis a directory. It’s probably not something you can read with aScanner.Change your condition to this: