forked from coderbruis/JavaSourceCodeLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMappedByteBufferTest.java
More file actions
29 lines (21 loc) · 788 Bytes
/
Copy pathMappedByteBufferTest.java
File metadata and controls
29 lines (21 loc) · 788 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.learnjava.io.nio;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
/**
* @author LuoHaiYang
*/
public class MappedByteBufferTest {
public static void main(String[] args) throws Exception {
RandomAccessFile randomAccessFile = new RandomAccessFile("1.txt", "rw");
// get channel
FileChannel channel = randomAccessFile.getChannel();
MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 5);
mappedByteBuffer.put(0, (byte)'H');
mappedByteBuffer.put(3, (byte)'9');
// IndexOutOfBoundsException
mappedByteBuffer.put(5, (byte)'Y');
randomAccessFile.close();
System.out.println("change success");
}
}