-
Notifications
You must be signed in to change notification settings - Fork 2
/
NIOTest.java
118 lines (114 loc) · 3.49 KB
/
NIOTest.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* 测试普通IO和NIO的异同
*/
/**
* @author Iver3oN Zhang
* @date 2016年3月15日
* @email [email protected] NIOTest.java Impossible is nothing
*/
public class NIOTest {
static private final byte message[] = { 83, 111, 109, 101, 32,
98, 121, 116, 101, 115, 46 };
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
NIORead();
//NIOWrite();
}
public static void NIOWrite(){
try {
FileOutputStream fout = new FileOutputStream(new File("NIOBooks.txt"));
FileChannel fc = fout.getChannel();
//创建一个1024字节的buffer
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.put("this is NIO test".getBytes());
//// 重设此缓冲区,将限制设置为当前位置,然后将当前位置设置为0 position为0 limit也为0 也就是 开始写入管道。
buffer.flip();
try {
fc.write( buffer );
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
fout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void NIORead(){
try {
@SuppressWarnings("resource")
FileInputStream fin = new FileInputStream("books.xml");
FileChannel fc = fin.getChannel();
//创建一个1024字节的buffer
// // 分配新的int缓冲区,参数为缓冲区容量
// 新缓冲区的当前位置将为零,其界限(限制位置)将为其容量。它将具有一个底层实现数组,其数组偏移量将为零。
ByteBuffer buffer = ByteBuffer.allocate(1024);
try {
//需要将数据从通道读到缓冲区中
fc.read(buffer);
//flip() 方法让缓冲区可以将新读入的数据写入另一个通道
buffer.flip();
// 查看在当前位置和限制位置之间是否有元素
while (buffer.remaining()>0) {
// 读取此缓冲区当前位置的整数,然后当前位置递增
byte b = buffer.get();
System.out.print(((char)b));
}
fin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void commonIO() {
// 普通IO方式读取book.xml数据
try {
InputStream in = new FileInputStream(new File("books.xml"));
BufferedReader br = new BufferedReader(new InputStreamReader(in));
OutputStream out = new FileOutputStream(new File("newbooks.xml"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
String temp = "";
// byte[] c = new byte[1024];
try {
while ((temp = br.readLine()) != null) {
// c = temp.getBytes();
System.out.println(temp);
bw.write(temp + "\n");
}
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}