在 Windows 中释放 Java 文件锁
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4179145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Release Java file lock in Windows
提问by jtnire
I'm having some problems deleting a file in Windows with java. For some reason, java is keeping a lock on my file, and I don't know why. Here is my code:
我在用 java 删除 Windows 中的文件时遇到了一些问题。出于某种原因,java 正在锁定我的文件,我不知道为什么。这是我的代码:
private byte[] getFileByteArray(File file) {
try {
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
try {
ByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
byte[] bt = new byte[buffer.remaining()];
buffer.get(bt);
channel.close();
raf.close();
file.delete();
return bt;
} catch (Exception ex) {
//Logger.getLogger(ConnectionImpl.class.getName()).log(Level.SEVERE, null, ex);
System.out.println(ex.toString());
}
} catch (FileNotFoundException ex) {
Logger.getLogger(ConnectionImpl.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
file.delete(), as well as trying manually in Explorer refuses to delete the file as it's still in use. All seems well in Linux though.
file.delete() 以及在资源管理器中手动尝试拒绝删除该文件,因为它仍在使用中。不过在 Linux 中一切似乎都很好。
Am I missing a close() somehwhere? I can confirm that the method which makes the file in the first place is closing the file, as I can delete the file before running the above code using file.delete()
我在某处错过了 close() 吗?我可以确认首先创建文件的方法是关闭文件,因为我可以在使用 file.delete() 运行上述代码之前删除文件
Extra Info:The code above is part of a method called getFileByteArray(File file) and is being called like this:
额外信息:上面的代码是名为 getFileByteArray(File file) 的方法的一部分,它的调用方式如下:
public byte[] createReport(int id) {
Report report = new Report();
String filename = report.CreateReport(id);
return getFileByteArray(new File(filename));
}
Thanks
谢谢
Update:I managed to fix the issue by reading the file kilobyte by kilobyte into the byte array using ByteArrayOutputStream
更新:我设法通过使用 ByteArrayOutputStream 将文件千字节读入字节数组来解决该问题
As a poster below mentioned, there is a known bug in Java in that Windows has issues with file mapping.
正如下面提到的海报,Java 中存在一个已知错误,即 Windows 存在文件映射问题。
回答by mhaller
This is a known Bug in Java on Windows, please see Bug #4715154
这是 Windows 上 Java 中的一个已知错误,请参阅错误 #4715154
Sun evaluated the problem and closed the bug with the following explanation:
Sun 评估了问题并关闭了错误,解释如下:
We cannot fix this. Windows does not allow a mapped file to be deleted. This problem should be ameliorated somewhat once we fix our garbage collectors to deallocate direct buffers more promptly (see 4469299), but otherwise there's nothing we can do about this.
我们无法解决这个问题。Windows 不允许删除映射文件。一旦我们修复了垃圾收集器以更迅速地释放直接缓冲区(参见 4469299),这个问题应该会有所改善,否则我们对此无能为力。
回答by Sero
And translating sarumont's comment into code
并将sarumont的评论翻译成代码
This should/may work.
这应该/可能工作。
private static void deleteMappedFilesIfExists(Path path) throws IOException {
while (true) {
try {
Files.deleteIfExists(path);
break;
} catch (AccessDeniedException e) {
System.gc();
}
//Add delay if needed.
}
}