在Windows中发布Java文件锁定 [英] Release Java file lock in Windows

查看:100
本文介绍了在Windows中发布Java文件锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用java删除一个文件在Windows中有一些问题。出于某种原因,java正在锁定我的文件,我不知道为什么。这是我的代码:

pre $ private $ byte getFileByteArray(File file){
try {
RandomAccessFile raf = new RandomAccessFile(file,r);
FileChannel channel = raf.getChannel();
尝试{

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;
$ b $ catch(Exception ex){
//Logger.getLogger(ConnectionImpl.class.getName()).log(Level.SEVERE,null,ex);
System.out.println(ex.toString());

$ b $ catch(FileNotFoundException ex){
Logger.getLogger(ConnectionImpl.class.getName()).log(Level.SEVERE,null,ex);
}
返回null;




$ b $ file.delete(),以及在资源管理器中手动尝试拒绝删除该文件仍在使用中。所有在Linux中似乎都很好。



我是否错过了close()somehwhere?我可以确认,使文件在第一个地方的方法是关闭文件,因为我可以在运行上面的代码之前使用file.delete()


   public byte [] createReport(int id){

Report report = new Report();
String filename = report.CreateReport(id);
return getFileByteArray(new File(filename));

$ / code $ / pre
$ b $ p感谢

更新:我设法解决这个问题,通过使用ByteArrayOutputStream读取文件千字节千字节到字节数组

作为一个海报下面提到,那里是Windows中已知的文件映射问题中的一个已知的bug。

解决方案

这是Windows上Java中已知的一个Bug,请参阅错误#4715154



Sun用以下解释评估了这个问题并解决了这个错误:

lockquote

我们无法解决这个问题。 Windows不允许删除映射的文件。这个
问题应该有所改善,一旦我们修复我们的垃圾收集器到
更快地释放直接缓冲区(见4469299),但除此之外有
我们无法做到这一点。 b $ b


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:

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.

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()

Extra Info: The code above is part of a method called getFileByteArray(File file) and is being called like this:

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

As a poster below mentioned, there is a known bug in Java in that Windows has issues with file mapping.

解决方案

This is a known Bug in Java on Windows, please see Bug #4715154

Sun evaluated the problem and closed the bug with the following explanation:

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中发布Java文件锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆