分别写入数据到序列化文件和文本文件中:
1. public static void main(String[] args) {
2. ArrayList al = new ArrayList();
3. try {
4. FileWriter fw = new FileWriter(new File("e://s.txt"));
5. for(int i=0;i<1000;i++){
6. String str = "000/t111/t222/t222/t222/t222/t222/t222/t222/t222/n";
7. al.add(str);
8. fw.write(str);
9. }
10. fw.close();
11. FileOutputStream fileStream = new FileOutputStream("e://s.obj");
12. ObjectOutputStream out = new ObjectOutputStream(fileStream);
13. out.writeObject(al);
14. out.close();
15. } catch (Exception e) {
16. e.printStackTrace();
17. }
18. }
19.
然后写程序读取序列化文件和文本文件,并将数据赋值到ArrayList中。
序列化读取:
1. public static void main(String[] args) {
2. ArrayList al = new ArrayList();
3. try {
4. long t = System.currentTimeMillis();
5. FileInputStream fileStream = new FileInputStream("e://s.obj");
6. BufferedInputStream br = new BufferedInputStream(fileStream);
7. ObjectInputStream in = new ObjectInputStream(br);
8. al = (ArrayList)in.readObject();
9. in.close();
10. System.out.println(System.currentTimeMillis()-t);
11. } catch (Exception e) {
12. e.printStackTrace();
13. }
14. }
15.
16. //文本文件读取:
17. public static void main(String[] args) {
18. ArrayList al = new ArrayList();
19. try {
20. long t = System.currentTimeMillis();
21. FileReader fw = new FileReader(new File("e://s.txt"));
22. BufferedReader br = new BufferedReader(fw);
23. String s = br.readLine();
24. while (s != null) {
25. al.add(s);
26. s = br.readLine();
27. }
28. br.close();
29. fw.close();
30. System.out.println(System.currentTimeMillis()-t);
31. } catch (Exception e) {
32. e.printStackTrace();
33. }
34.
35. }
36.
37.
结论:
在行数i=1000时,序列化读取平均32,文本文件读取平均16。
在行数i=10000时,序列化读取平均46,文本文件读取平均46。
在行数i=100000时,序列化读取平均96,文本文件读取平均316。
故在大数据量读取时,使用序列化方式存取数据,效率较高;而在小数据量(小于10000行)时,使用文本文件存取数据效率较高。
1. public static void main(String[] args) {
2. ArrayList al = new ArrayList();
3. try {
4. FileWriter fw = new FileWriter(new File("e://s.txt"));
5. for(int i=0;i<1000;i++){
6. String str = "000/t111/t222/t222/t222/t222/t222/t222/t222/t222/n";
7. al.add(str);
8. fw.write(str);
9. }
10. fw.close();
11. FileOutputStream fileStream = new FileOutputStream("e://s.obj");
12. ObjectOutputStream out = new ObjectOutputStream(fileStream);
13. out.writeObject(al);
14. out.close();
15. } catch (Exception e) {
16. e.printStackTrace();
17. }
18. }
19.
然后写程序读取序列化文件和文本文件,并将数据赋值到ArrayList中。
序列化读取:
1. public static void main(String[] args) {
2. ArrayList al = new ArrayList();
3. try {
4. long t = System.currentTimeMillis();
5. FileInputStream fileStream = new FileInputStream("e://s.obj");
6. BufferedInputStream br = new BufferedInputStream(fileStream);
7. ObjectInputStream in = new ObjectInputStream(br);
8. al = (ArrayList)in.readObject();
9. in.close();
10. System.out.println(System.currentTimeMillis()-t);
11. } catch (Exception e) {
12. e.printStackTrace();
13. }
14. }
15.
16. //文本文件读取:
17. public static void main(String[] args) {
18. ArrayList al = new ArrayList();
19. try {
20. long t = System.currentTimeMillis();
21. FileReader fw = new FileReader(new File("e://s.txt"));
22. BufferedReader br = new BufferedReader(fw);
23. String s = br.readLine();
24. while (s != null) {
25. al.add(s);
26. s = br.readLine();
27. }
28. br.close();
29. fw.close();
30. System.out.println(System.currentTimeMillis()-t);
31. } catch (Exception e) {
32. e.printStackTrace();
33. }
34.
35. }
36.
37.
结论:
在行数i=1000时,序列化读取平均32,文本文件读取平均16。
在行数i=10000时,序列化读取平均46,文本文件读取平均46。
在行数i=100000时,序列化读取平均96,文本文件读取平均316。
故在大数据量读取时,使用序列化方式存取数据,效率较高;而在小数据量(小于10000行)时,使用文本文件存取数据效率较高。