Skip to content

Commit 212ff13

Browse files
committed
添加一个NIO的例子
1 parent 1fa4408 commit 212ff13

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cn.byhieg.niotutorial;
2+
3+
import java.io.File;
4+
import java.io.FileNotFoundException;
5+
import java.io.IOException;
6+
import java.io.RandomAccessFile;
7+
import java.nio.ByteBuffer;
8+
import java.nio.channels.FileChannel;
9+
10+
/**
11+
* Created by shiqifeng on 2017/4/10.
12+
13+
*/
14+
public class NioTest {
15+
16+
public static void main(String[] args) throws IOException {
17+
String s = File.separator;
18+
RandomAccessFile aFile = new RandomAccessFile("D:" + s + "read_file.txt","rw");
19+
FileChannel inChannel = aFile.getChannel();
20+
21+
ByteBuffer buf = ByteBuffer.allocate(48);
22+
int bytesRead = inChannel.read(buf);
23+
while (bytesRead != -1) {
24+
buf.flip();
25+
while (buf.hasRemaining()) {
26+
System.out.print((char) buf.get());
27+
}
28+
29+
buf.clear();
30+
bytesRead = inChannel.read(buf);
31+
}
32+
33+
aFile.close();
34+
}
35+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
I don't know what I do now is right, those are wrong, and when I finally die then I know these.
2+
So what I can do now is to try to do everything well, and then wait to die .
3+
Sometimes I can be very happy to talk to everyone, can be very presumptuous, but no one knows,
4+
it is but very deliberately camouflage, camouflage; I can make him very happy very happy,
5+
but couldn't find the source of happiness, just giggle.

0 commit comments

Comments
 (0)