java:24G文件写入所要时间23.9分,读取只有67秒(1G内存测试)

转载:http://www.cnblogs.com/qiaolevip/archive/2010/05/17/1737219.html(该博客有多篇关于大数据读取,合并,拆分的有效方法)

另附上readline ready的原理:http://blog.csdn.net/neusoftware_20063500/article/details/3723176

已测,确实很快

 1 package arrays.file;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.FileReader;
6 import java.io.FileWriter;
7 import java.io.IOException;
8
9 public class WriteFile {
10 public static void main(String[] args) {
11 String path = "d:/data.txt";
12 writerFile(path);
13 readerFile(path);
14 }
15
16 // 写入文件
17 public static void writerFile(String path) {
18 try {
19 BufferedWriter bw = new BufferedWriter(new FileWriter(path));
20
21 // int max = Integer.MAX_VALUE;
22 int max = 1000;
23 System.out.println("总写入文件行数:" + max);
24 long start = System.currentTimeMillis();
25
26 for (int i = 0; i < max; i++) {
27 bw.write((int) (Math.random() * max) + "");
28 bw.newLine();
29 }
30
31 bw.close();
32
33 long end = System.currentTimeMillis();
34 System.out.println("写入耗时:" + (end - start) + "ms = "
35 + ((double) (end - start) / 1000) + "s");
36 } catch (IOException e) {
37 e.printStackTrace();
38 }
39 }
40
41 // 读取文件
42 public static void readerFile(String path) {
43 try {
44 BufferedReader br = new BufferedReader(new FileReader(path));
45
46 long start = System.currentTimeMillis();
47
48 while (br.ready()) {
49 br.readLine();
50 }
51
52 br.close();
53
54 long end = System.currentTimeMillis();
55 System.out.println("读取耗时:" + (end - start) + "ms = "
56 + ((double) (end - start) / 1000) + "s");
57 } catch (IOException e) {
58 e.printStackTrace();
59 }
60 }
61
62 }

 

 

 

posted @ 2012-03-12 15:20  carlosfu  阅读(378)  评论(0编辑  收藏  举报