The DirectByteBuffer mentioned in the previous article inherits from MappedByteBuffer
The definition of MappedByteBuffer:
A direct byte buffer whose content is a memory-mapped region of a file.
Direct cache, the content is a memory mapped file.
Create a test class
public class NioTest9 { public static void main(String[] args) throws Exception { RandomAccessFile randomAccessFile = new RandomAccessFile("NioTest9.txt","rw"); FileChannel fileChannel = randomAccessFile.getChannel(); MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0,5); mappedByteBuffer.put(0,(byte)‘a’); mappedByteBuffer.put(3,(byte)‘b’); randomAccessFile.close(); } }
Create NioTest9.tex file
Run the program and open it with Notepad
The operation is off-heap memory, off-heap memory Writing to the file is controlled by the operating system.
public class NioTest9 { public static void main(String[] args) throws Exception { RandomAccessFile randomAccessFile = new RandomAccessFile("NioTest9.txt","rw"); FileChannel fileChannel = randomAccessFile.getChannel(); MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0,5); mappedByteBuffer.put(0,(byte)‘a’); mappedByteBuffer.put(3,(byte)‘b’); randomAccessFile.close(); } }