Young87

当前位置:首页 >个人收藏

MappedByteBuffer 高效读写文件

    private static final int start = 0;
    public static void main(String[] args) throws IOException {

        RandomAccessFile raf = new RandomAccessFile("D:/mapper.txt", "rw");
        FileChannel fileChannel = raf.getChannel();

        String a = "我要写入文件";
        MappedByteBuffer mbb = fileChannel.map(FileChannel.MapMode.READ_WRITE, start, a.getBytes().length+1);
        mbb.put(a.getBytes());

        mbb.flip();

        byte[] bb = new byte[mbb.capacity()];
        while (mbb.hasRemaining()){
            byte b = mbb.get();
            bb[mbb.position()]=b;
        }
        System.out.println(new String(bb));
        raf.close();

    }

除特别声明,本站所有文章均为原创,如需转载请以超级链接形式注明出处:SmartCat's Blog

上一篇: 「开源」10个优秀的SpringBoot项目,总有你喜欢的

下一篇: RocketMQ记录MMAP、PageCache的一些相关知识

精华推荐