MappedByteBuffer
内存管理
MappedByteBuffer
示例
public static void main(String[] args) throws HttpProcessException, NoSuchMethodException {
File file = new File("E://data.txt");
long len = file.length();
byte[] ds = new byte[(int) len];
try {
MappedByteBuffer mappedByteBuffer = new RandomAccessFile(file, "rw")
.getChannel()
//读写的方式将文件map到虚拟内存
.map(FileChannel.MapMode.READ_WRITE, 0, len);
for (int offset = 0; offset < len; offset++) {
//从MappedByteBuffer中读数据
byte b = mappedByteBuffer.get();
ds[offset] = b;
}
System.out.println(new String(ds));
for (int offset = 0; offset < len; offset++) {
//往MappedByteBuffer中写数据
mappedByteBuffer.put(offset, ds[(int)len - offset -1]);
}
//刷新到磁盘
mappedByteBuffer.force();
} catch (IOException e) {
e.printStackTrace();
}
}MapMode类型
分析
注意
MappedByteBuffer与DirectByteBuffer的区别
Last updated