forked from fengshao0907/JavaCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChannelTest.java
More file actions
29 lines (22 loc) · 786 Bytes
/
Copy pathChannelTest.java
File metadata and controls
29 lines (22 loc) · 786 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.NIO.core;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class ChannelTest {
public static void main(String[] args) throws Exception {
RandomAccessFile file = new RandomAccessFile("E:/workspace/channelTest.txt","rw");
FileChannel fileChannel = file.getChannel();
ByteBuffer buf = ByteBuffer.allocate(8);
int bytesRead = fileChannel.read(buf);
while (bytesRead != -1){
System.out.println("Read " + bytesRead);
buf.flip();
while (buf.hasRemaining()){
System.out.println((char)buf.get());
}
buf.clear();
bytesRead = fileChannel.read(buf);
}
file.close();
}
}