Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Java编写和读取MappedByteBuffer_Java_Nio_Memory Mapped Files_Mappedbytebuffer - Fatal编程技术网

用Java编写和读取MappedByteBuffer

用Java编写和读取MappedByteBuffer,java,nio,memory-mapped-files,mappedbytebuffer,Java,Nio,Memory Mapped Files,Mappedbytebuffer,我正在学习java中的内存映射文件。 我想知道如何写/读地图 private static void write(String strFilePath) { File fl = new File(strFilePath); FileChannel fChannel = null; RandomAccessFile rf = null; MappedByteBuffer mBBuffer = null; try {

我正在学习java中的内存映射文件。 我想知道如何写/读地图

private static void write(String strFilePath) {

        File fl = new File(strFilePath);
        FileChannel fChannel = null;
        RandomAccessFile rf = null;
        MappedByteBuffer mBBuffer = null;
        try {

            rf = new RandomAccessFile(fl, "rw");
            fChannel = rf.getChannel();

            mBBuffer = fChannel.map(FileChannel.MapMode.READ_WRITE, 0, 1024  );

            for(int i =0;i<10030;i++) {

                if(i == mBBuffer.limit()) {
                    mBBuffer = fChannel.map(FileChannel.MapMode.READ_WRITE,i+1,1024);
                }
                mBBuffer.put((byte)'a');
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if(fChannel != null) {
                    fChannel.close();
                }
                if(rf != null) {
                    rf.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
这是我用来编写MappedByteBuffer的代码

private static void write(String strFilePath) {

        File fl = new File(strFilePath);
        FileChannel fChannel = null;
        RandomAccessFile rf = null;
        MappedByteBuffer mBBuffer = null;
        try {

            rf = new RandomAccessFile(fl, "rw");
            fChannel = rf.getChannel();

            mBBuffer = fChannel.map(FileChannel.MapMode.READ_WRITE, 0, 1024  );

            for(int i =0;i<10030;i++) {

                if(i == mBBuffer.limit()) {
                    mBBuffer = fChannel.map(FileChannel.MapMode.READ_WRITE,i+1,1024);
                }
                mBBuffer.put((byte)'a');
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if(fChannel != null) {
                    fChannel.close();
                }
                if(rf != null) {
                    rf.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
私有静态无效写入(字符串strFilePath){
文件fl=新文件(strFilePath);
FileChannel fcchannel=null;
随机存取文件rf=null;
MappedByteBuffer mBBuffer=null;
试一试{
rf=新随机存取文件(fl,“rw”);
fcchannel=rf.getChannel();
mBBuffer=fcchannel.map(FileChannel.MapMode.READ_WRITE,0,1024);

对于(int i=0;iYou通常会映射整个文件,而不是一次映射1k和所有其他的恶作剧。谢谢你的回答,那么我该如何编写一个大文件?应该将什么作为参数传递给FileChannel.map()函数?它会提前要求一个大小(我不知道)你根本不会用
MappedByteBuffer
来写它,这没有什么好处。