如何检查文件是否可读? [英] How to check if a file is readable?
问题描述
我正在编写Java 6应用程序,我必须检查文件是否可读。但是,在Windows上 canRead()
始终返回 true
。所以我看到可能,唯一的解决方案可能是基于WINAPI并用JNA / JNI编写的一些原生解决方案。
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 probably, the only solution could be some native solution based on WINAPI and written in JNA/JNI.
但是,还有另一个问题,因为在WINAPI中很难找到一个简单的函数,它会返回有关文件访问的信息。我发现 GetNamedSecurityInfo
或 GetSecurityInfo
但我不是一个高级的WINAPI程序员,它们对我来说太复杂了JNA / JNI。有什么想法如何处理这个问题?
But, there is another problem, because it's difficult to find a simple function in WINAPI which would return information about access to a file. I found GetNamedSecurityInfo
or GetSecurityInfo
but I'm not an advanced WINAPI programmer and they are too complicated for me in connection with JNA/JNI. Any ideas how to deal with this problem?
推荐答案
尝试使用以下代码
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;
}
这篇关于如何检查文件是否可读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!