java 数据流对比_几种Java读写数据的流性能对比

近来,在做服务器后台处理数据的时候,需要用到Java自带的几种流对数据进行读写,初始时没怎么在意,就随便用了一个,结果发现性能上并不尽如人意。于是对几种常用的流做了个小小的性能测试。测试代码如下:

1 public static int FileOutputStreamTime = 0;2 public static int BufferedOutputStreamTime = 0;3 public static int FileWriterTime = 0;4 public static int FileInputStreamTime = 0;5 public static int BufferedInputStreamTime = 0;6 public static int FileReaderTime = 0;7

8 public static voidwrite(String filePath, String content){9 FileOutputStream out = null;10 FileOutputStream outStr = null;11 BufferedOutputStream buf = null;12 FileWriter fw = null;13 File f = newFile(filePath);14

15 try{16 //Test FileOutputStream

17 long begin1 =System.currentTimeMillis();18 out = newFileOutputStream(f);19 out.write(content.getBytes());20 out.close();21 long end1 =System.currentTimeMillis();22 FileOutputStreamTime += end1 -begin1;23

24 //Test BufferedOutputStream

25 long begin2 =System.currentTimeMillis();26 outStr = newFileOutputStream(f);27 buf = newBufferedOutputStream(outStr);28 buf.write(content.getBytes());29 buf.flush();30 buf.close();31 long end2 =System.currentTimeMillis();32 BufferedOutputStreamTime += end2 -begin2;33

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值