我需要检查给定的二进制文件是否具有写访问权限.文件类API有一个错误,它在JDK7中修复,但我不能只升级到它.
以下是该错误的链接:http: //bugs.sun.com/bugdatabase/view_bug.do?video_id = 6203387
当我打开FileOutputStream时,它会破坏二进制文件,资源管理器会将其大小显示为零并且无法启动它.这是代码片段.
操作系统:Win7
请帮助我理解为什么只打开输出流(不写任何东西)会破坏二进制文件.有没有解决这个问题的方法?
这是代码片段:
private boolean hasWriteAccess(File file) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
} catch (Exception e) {
e.printStackTrace();
if(fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)