java序列化读取与文本文件读取数据效率对比

分别写入数据到序列化文件和文本文件中:

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行)时,使用文本文件存取数据效率较高。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

meteor_730

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值