高手的存在,就是让服务10亿人的时候,你感觉只是为你一个人服务......

字节流与字符流文件读写性能比较

目录

这里对java字节流与字符流文件读写进行简单的性能测试,分为以下四种方式:

  1. 字节读写(InputStream/OutputStream)
  2. 字符读取(FileReader/FileWriter)
  3. 行读取(BufferedReader/BufferedWriter)
  4. NIO(FileChannel&&ByteBuffer)

从实现原理上来看nio>BufferedReader&&FileReader>InputStream


代码如下:读写的文件为jdk 1.6的api文档,
JDK_API_1_6_zh_CN.CHM ,35M大小

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package io;

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.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;

;
public class FileFerformanceTest {

public static void main(String[] args) {

String intPutFile = "c://JDK_API_1_6_zh_CN.CHM";
String outputFile = "c://output.txt";

/**
* 字节流读写文件测试
*/

{
long fileInputStreamBegin = getTime();
fileInputStream(intPutFile, outputFile);
long fileInputStreamEnd = getTime();
System.out.println("InputStream--字节流读写时间花费:"
+ (fileInputStreamEnd - fileInputStreamBegin));
}

/**
* 字符流读写文件
*/

{
long filefileReaderBegin = getTime();
fileReader(intPutFile, outputFile);
long filefileReaderEnd = getTime();
System.out.println("fileReader--字符流读写时间花费:"
+ (filefileReaderEnd - filefileReaderBegin));
}

/**
* 字符逐行读取
*/

{
long filebufferedReaderBegin = getTime();
bufferedReader(intPutFile, outputFile);
long filebufferedReaderEnd = getTime();
System.out.println("bufferedReader--字符流逐行读写时间花费:"
+ (filebufferedReaderEnd - filebufferedReaderBegin));
}

/**
* nio读写文件
*/

{
long filenioChannelBegin = getTime();
nioChannel(intPutFile, outputFile);
long filenioChannelEnd = getTime();
System.out.println("nio--读写时间花费:"
+ (filenioChannelEnd - filenioChannelBegin));
}
}

private static long getTime() {

return System.currentTimeMillis();
}

/**
* 字节流读写文件文件
*
* @param intPutFile
* @param outputFile
*/

private static void fileInputStream(String intPutFile, String outputFile) {

InputStream iputStream = null;
OutputStream outputStream = null;
File file = new File(intPutFile);
int temp = 0;

try {
iputStream = new FileInputStream(file);
outputStream = new FileOutputStream(outputFile);

while ((temp = iputStream.read()) != -1) {

outputStream.write(temp);

}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
iputStream.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

/**
* 字符流读写文件
*
* @param intPutFile
* @param outputFile
*/

private static void fileReader(String intPutFile, String outputFile) {

File file = new File(intPutFile);
FileReader fileReader = null;
FileWriter fileWriter = null;

try {
fileReader = new FileReader(file);
fileWriter = new FileWriter(outputFile);
int temp = 0;

while ((temp = fileReader.read()) != -1) {

fileWriter.write(temp);
}

} catch (Exception e) {
e.printStackTrace();
} finally {

try {
fileReader.close();
fileWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}

}

/**
* 字符逐行读取
*
* @param intPutFile
* @param outputFile
*/

private static void bufferedReader(String intPutFile, String outputFile) {

File file = new File(intPutFile);

BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;

try {
bufferedReader = new BufferedReader(new FileReader(file));
bufferedWriter = new BufferedWriter(new FileWriter(outputFile));

String temp = null;
while ((temp = bufferedReader.readLine()) != null) {
bufferedWriter.write(temp + "\n");
}

} catch (Exception e) {
e.printStackTrace();
} finally {

try {
bufferedReader.close();
bufferedWriter.close();
} catch (Exception e) {
e.printStackTrace();
}

}

}

/**
* nio读写文件
*
* @param intPutFile
* @param outputFile
*/

private static void nioChannel(String intPutFile, String outputFile) {
File file = new File(intPutFile);

FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
FileChannel fileChannelIn = null;
FileChannel fileChannelOut = null;
int capacity = 1024;// 字节
ByteBuffer buffer = ByteBuffer.allocate(capacity);

int temp = 0;
try {

fileInputStream = new FileInputStream(file);
fileOutputStream = new FileOutputStream(outputFile);
fileChannelIn = fileInputStream.getChannel();
fileChannelOut = fileOutputStream.getChannel();

while (true) {
// 将当前位置设置为0,限制设置为容量,目的是尽最大可能让字节,由通道读取到缓冲中
buffer.clear();
// 从输入通道中将数据读到缓冲区
int r = fileChannelIn.read(buffer);
if (r == -1) {
break;
}
// 当前位置置为限制,然后将当前位置置为0,目的是将有数据部分的字节,由缓冲写入到通道中。通常用在读与写之间
buffer.flip();
fileChannelOut.write(buffer);
}

} catch (Exception e) {
e.printStackTrace();
} finally {

try {
fileInputStream.close();
fileOutputStream.close();

} catch (Exception e) {
e.printStackTrace();
}
}

}
}

结果如下:

Alt text