-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileOperate.java
More file actions
375 lines (359 loc) · 8.6 KB
/
FileOperate.java
File metadata and controls
375 lines (359 loc) · 8.6 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/**
*
*/
package com.fileoperate;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
/**
* @author dell
*
*/
public class FileOperate {
// public static Logger logger = Logger.getLogger("INFO");
/**
*
*/
public FileOperate() {
}
/**
* 字节流读取,会遇到中文乱码问题
*
* @deprecated
* @param filePath
*/
public static long readFileByByte(String filePath) {
long start = System.currentTimeMillis();
InputStream is = null;
try {
is = new FileInputStream(filePath);
int tmp;
StringBuffer sb = new StringBuffer();
// read() 来自native的read0方法
while ((tmp = is.read()) != -1) {
sb.append((char) tmp);
}
System.out.println(sb);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return System.currentTimeMillis() - start;
}
/**
* 定长字节流读取,会遇到中文乱码问题.无法避开半中文字问题.
*
* @param filePath
*/
public static long readFileByByte(String filePath, int n) {
long start = System.currentTimeMillis();
InputStream is = null;
try {
is = new FileInputStream(filePath);
int tmp;
StringBuffer sb = new StringBuffer();
byte[] b = new byte[n];
while ((tmp = is.read(b)) != -1) {
sb.append(new String(b, 0, tmp));
}
System.out.println(sb);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return System.currentTimeMillis() - start;
}
/**
* 字符读取
*
* @param filePath
*/
public static long readFileByCharacter(String filePath) {
long start = System.currentTimeMillis();
FileReader fr = null;
try {
fr = new FileReader(new File(filePath));
int tmp;
StringBuffer sb = new StringBuffer();
while ((tmp = fr.read()) != -1) {
sb.append((char) tmp);
}
System.out.println(sb);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fr != null) {
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return System.currentTimeMillis() - start;
}
/**
* 读取定长字符
*
* @param filePath
*/
public static long readFileByCharacter(String filePath, int n) {
long start = System.currentTimeMillis();
FileReader fr = null;
try {
fr = new FileReader(new File(filePath));
int tmp;
StringBuffer sb = new StringBuffer();
char[] c = new char[n];
while ((tmp = fr.read(c)) != -1) {
sb.append(c, 0, tmp);
}
System.out.println(sb);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fr != null) {
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return System.currentTimeMillis() - start;
}
/**
* 行读取都是以char来操作的
*
* @param filePath
*/
public static void readFileByLine(String filePath) {
//
BufferedReader br = null;
//
StringBuffer sb = new StringBuffer();
//
String tmp = null;
try {
br = new BufferedReader(new FileReader(new File("filePath")));
while ((tmp = br.readLine()) != null) {
sb.append(tmp);
}
System.out.println(sb);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 5M緩存行读取. 2G以上大文件推荐 原理就是以5M进行行读取。
*
* @param filePath
*/
public static long readLargeFileByByte(String filePath, int n,
String outputFilePath) {
long start = System.currentTimeMillis();
BufferedInputStream bis = null;
BufferedReader bfr = null;
FileWriter fileWriter = null;
String line;
try {
bis = new BufferedInputStream(new FileInputStream(
new File(filePath)));
bfr = new BufferedReader(new InputStreamReader(bis, "UTF-8"), n);
fileWriter = new FileWriter(new File(outputFilePath));
StringBuffer sb = new StringBuffer();
Map<String, Integer> map = new HashMap<String, Integer>();
while ((line = bfr.readLine()) != null) {
// fileWriter.append(sb.append(line + "\n"));
if (map.containsKey(line.trim())) {
map.put(line.trim(), map.get(line.trim()) + 1);
} else {
map.put(line.trim(), 1);
}
}
// iterateSet(map);
// forEach(map);
fileWriter.append(entrySet(map));
// System.out.println(sb.toString());
fileWriter.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bis != null) {
bis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bfr != null) {
bfr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fileWriter != null) {
fileWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return System.currentTimeMillis() - start;
}
/**
* NIO Byte读取
*
* @param filePath
*/
public static long readFileByNIO(String filePath, String fileOutPath, int n) {
long start = System.currentTimeMillis();
ByteBuffer bb = null;
FileInputStream fis = null;
// FileOutputStream fos = null;
FileChannel fc = null;
// FileChannel fc2 = null;
try {
fis = new FileInputStream(new File(filePath));
// fos = new FileOutputStream(new File(fileOutPath));
fc = fis.getChannel();
// FileChannel fc2 = fos.getChannel();
bb = ByteBuffer.allocate(n);
while (fc.read(bb) != -1) {
bb.clear();
bb.flip();
// fc2.write(bb);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fc != null) {
fc.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return System.currentTimeMillis() - start;
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("大文件行读取[]: "
// iplog.20170825
+ readLargeFileByByte("D:/相关文档/利农商城/iplog.20170825",
// 5 * 1024* 1024
5, "C:/Users/dell/Desktop/accesslog.170812") + "ms");
/**
* System.out.println("流读取[]: " +
* readFileByCharacter("C:/Users/dell/Desktop/accesslog.170810", 1024) +
* "ms");
*/
/**
* System.out.println("行读取[]: " +
* readFileByCharacter("C:/Users/dell/Desktop/accesslog.170810", 1024) +
* "ms");
*/
// readFileByLine
}
/**
* entrySet遍历用时:44526672ns
*
* @param map
*/
public static StringBuffer entrySet(Map<String, Integer> map) {
long start = System.nanoTime();
StringBuffer sb = new StringBuffer();
for (Map.Entry<String, Integer> entry : map.entrySet()) {
// System.out.println(entry.getKey() + "|" + entry.getValue());
sb.append(entry.getKey() + "|" + entry.getValue() + "\n");
}
System.out
.println("entrySet遍历用时:" + (System.nanoTime() - start) + "ns");
return sb;
}
/**
* iterateSet用时:1425709ns
*
* @param map
*/
public static StringBuffer iterateSet(Map<String, Integer> map) {
long start = System.nanoTime();
StringBuffer sb = new StringBuffer();
Iterator<Entry<String, Integer>> iter = map.entrySet().iterator();
Map.Entry<String, Integer> entry;
while (iter.hasNext()) {
entry = iter.next();
// System.out.println(entry.getKey() + "|" + entry.getValue());
}
System.out
.println("iterateSet用时:" + (System.nanoTime() - start) + "ns");
return sb;
}
/**
* entrySet遍历用时:44526672ns forEach遍历用时:127330675ns
*
* @param map
*/
public static StringBuffer forEach(Map<String, Integer> map) {
long start = System.nanoTime();
StringBuffer sb = new StringBuffer();
map.forEach((k, v) -> {
// System.out.println(k + "|" + v);
});
System.out.println("forEach遍历用时:" + (System.nanoTime() - start) + "ns");
return sb;
}
}