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

    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章