本文介绍了如何正确关闭MappedByteBuffer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在运行的代码:

  import java.io.RandomAccessFile; 
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

公共类Main {
public static void main(String [] args)throws Exception {
String filePath =D:/ temp / file;
RandomAccessFile文件= new RandomAccessFile(filePath,rw);

try {
MappedByteBuffer buffer = file.getChannel()。map(FileChannel.MapMode.READ_WRITE,0,128);

//做一些事情
buffer.putInt(4);
} finally {
file.close();
System.out.println(文件关闭);
}

System.out.println(按任意键......);
System.in.read();

System.out.println(已完成);
}
}

在按键之前,我正在尝试删除该文件在FAR Manager中手动完成。但FAR表示该文件已被锁定:

 该进程无法访问该文件,因为该文件正由另一个进程使用。 
无法删除文件
D:\ temp \ file
正在打开对象:
Java(TM)Platform SE二进制文件(PID:5768,C:\ Program Files\Java\jdk1.8.0_05\bin\javaw.exe)

仅限按下一个键后,应用程序终止,我可以删除该文件。



我的代码有什么问题?

 公共类测试
{
public static void main(String [] args)throws Exception {
String filePath =D:/ temp / file;
RandomAccessFile文件= new RandomAccessFile(filePath,rw);
FileChannel chan = file.getChannel();
try {
MappedByteBuffer buffer = chan.map(FileChannel.MapMode.READ_WRITE,0,128);

//做一些事情
buffer.putInt(4);
buffer.force();
Cleaner cleaner =((sun.nio.ch.DirectBuffer)buffer).cleaner();
if(cleaner!= null){
cleaner.clean();
}
} finally {
chan.close();
file.close();
System.out.println(文件关闭);
}

System.out.println(按任意键......);
System.in.read();

System.out.println(已完成);
}
}


This is the code I'm running:

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
    public static void main(String[] args) throws Exception {
        String filePath = "D:/temp/file";
        RandomAccessFile file = new RandomAccessFile(filePath, "rw");

        try {
            MappedByteBuffer buffer = file.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 128);

            // Do something
            buffer.putInt(4);
        } finally {
            file.close();
            System.out.println("File closed");
        }

        System.out.println("Press any key...");
        System.in.read();

        System.out.println("Finished");
    }
}

Before pressing a key, I'm trying to delete the file manually in FAR Manager. But FAR says that the file is locked:

 The process cannot access the file because it is being used by another process.
                     Cannot delete the file
                         D:\temp\file
                    Object is being opened in:
 Java(TM) Platform SE binary (PID: 5768, C:\Program Files\Java\jdk1.8.0_05\bin\javaw.exe)

Only after pressing a key, the application terminates and I can delete the file.

What is wrong with my code?

解决方案

Try this one.

public class Test
{
    public static void main(String[] args) throws Exception {
        String filePath = "D:/temp/file";
        RandomAccessFile file = new RandomAccessFile(filePath, "rw");
        FileChannel chan = file.getChannel();
        try {
            MappedByteBuffer buffer = chan.map(FileChannel.MapMode.READ_WRITE, 0, 128);

            // Do something
            buffer.putInt(4);
            buffer.force();
            Cleaner cleaner = ((sun.nio.ch.DirectBuffer) buffer).cleaner();
            if (cleaner != null) {
                cleaner.clean();
            }
        } finally {
            chan.close();
            file.close();
            System.out.println("File closed");
        }

        System.out.println("Press any key...");
        System.in.read();

        System.out.println("Finished");
    }
}

这篇关于如何正确关闭MappedByteBuffer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:46