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

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