I\'m writing Java 6 application and I have to check if a file is readable. However, on Windows canRead()
always returns true
. So I see that probabl
You only need to know if it's readable if you are going to read it. So, just try to read it, when you need to, and deal with it if you can't. The best way to test the availability of any resource is just to try to use it and deal with the exceptions or errors as they arise. Don't try to predict the future.
Java 7 introduced the Files.isReadable
static method, it accepts a file Path and returns true if file exists and is readable, otherwise false.
From the docs
Tests whether a file is readable. This method checks that a file exists and that this Java virtual machine has appropriate privileges that would allow it open the file for reading. Depending on the implementation, this method may require to read file permissions, access control lists, or other file attributes in order to check the effective access to the file. Consequently, this method may not be atomic with respect to other file system operations.
Note that the result of this method is immediately outdated, there is no guarantee that a subsequent attempt to open the file for reading will succeed (or even that it will access the same file). Care should be taken when using this method in security sensitive applications.
Example:
File file = new File("/path/to/file");
Files.isReadable(file.toPath()); // true if `/path/to/file` is readable
You could use FilePermission and AccessController tougher in this way :
FilePermission fp = new FilePermission("file.txt", "read");
AccessController.checkPermission(fp);
If a requested access is allowed, checkPermission returns quietly. If denied, an AccessControlException is thrown.
It can be perfectly reasonable to check accessibility of a resource (readability of a file in this case) long before the actual usage of that resource.
Imagine a server application which will use a subcomponent which will read a particular file in some circumstances some time later. It can be useful to check the readability of the file at server startup and WARN if it's not readable. Then somebody can fix the situation (make the file readable, anything) before the circumstances actually causes the subcomponent to try to read that file.
Of course, this upfront check is not a substitute for proper exception handling in the sub component (it's very possible that the file is readable at start but became unreadable later).
So the question about pre-checking is perfectly valid I think.
As for the method of checking the resource, try to do something as similar as possible to actual usage. If later the file will be read, then try to read it!
As for Files.isReadable(...): there's no guarantee that the file system provider beneath Files.isReadable(...) is not buggy. It can return true, and then throw an exception if the file is actually read.
Of course, if you are writing a file manager application then use Files.isReadable(...), but I guess that is not the case.
Try to use the following code
public boolean checkFileCanRead(File file){
try {
FileReader fileReader = new FileReader(file.getAbsolutePath());
fileReader.read();
fileReader.close();
} catch (Exception e) {
LOGGER.debug("Exception when checking if file could be read with message:"+e.getMessage(), e);
return false;
}
return true;
}