什么是mmap
Java中mmap的使用,具体参考kafka源码中的OffsetIndex这个类
操作文件,就相当于操作一个ByteBuffer一样
public class TestMmap {
public static String path = "C:\\Users\\64371\\Desktop\\mmap";
public static void main(String[] args) throws IOException {
File file1 = new File(path, "1");
RandomAccessFile randomAccessFile = new RandomAccessFile(file1, "rw");
int len = 2048;
// 映射为2kb,那么生成的文件也是2kb
MappedByteBuffer mmap = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, len);
System.out.println(mmap.isReadOnly());
System.out.println(mmap.position());
System.out.println(mmap.limit());
// 写数据之后,JVM 退出之后会强制刷新的
mmap.put("a".getBytes());
mmap.put("b".getBytes());
mmap.put("c".getBytes());
mmap.put("