Search code examples
javafilefile-permissions

check if a folder is readable in Java 1.6


I am trying to check if a folder is readable in Java 1.6 with the following two manner:

1) Using canRead method of File class. But it's readable all the time (canRead() return always true):

final File folder = new File("file.xml");
if(folder.canRead()){
   // The file is readable
}else{

  // The file is not readable!!
}

2) Using FilePermission class and catch exception. But it catchs the exception all the time (when the folder is readable or not):

try {

  FilePermission filePermission = new FilePermission(folder.getAbsolutePath(), "read");
AccessController.checkPermission(filePermission);

// The file is readable


} catch (AccessControlException pACE) {
 // The file is not readable !!
} 

I have found that there is an issue between Microsoft Windows OS and Java 1.6 for this case.

Bug: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6203387

Have you any solution?


Solution

  • This is quick and dirty,

    File dir = new File("foo");
    if (dir.exists()) {
       if (dir.listFiles() == null) {
          // directory not readable
       }
    }
    

    all the IO errors are handled inside of listFiles().