forked from byhieg/JavaTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNioTest.java
More file actions
35 lines (29 loc) · 927 Bytes
/
NioTest.java
File metadata and controls
35 lines (29 loc) · 927 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
30
31
32
33
34
35
package cn.byhieg.niotutorial;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* Created by shiqifeng on 2017/4/10.
* Mail byhieg@gmail.com
*/
public class NioTest {
public static void main(String[] args) throws IOException {
String s = File.separator;
RandomAccessFile aFile = new RandomAccessFile("D:" + s + "read_file.txt","rw");
FileChannel inChannel = aFile.getChannel();
ByteBuffer buf = ByteBuffer.allocate(48);
int bytesRead = inChannel.read(buf);
while (bytesRead != -1) {
buf.flip();
while (buf.hasRemaining()) {
System.out.print((char) buf.get());
}
buf.clear();
bytesRead = inChannel.read(buf);
}
aFile.close();
}
}