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

展开全部

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 void write(String filePath, String content){ 9 FileOutputStream out = null; 10 FileOutputStream outStr = null; 11 BufferedOutputStream buf = null; 12 FileWriter fw = null; 13 File f = new File(filePath); 14 15 try { 16 //Test FileOutputStream 17 long begin1 = System.currentTimeMillis();

18 out = new FileOutputStream(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 = new FileOutputStream(f); 27 buf = new BufferedOutputStream(outStr); 28 buf.write(content.getBytes()); 29 buf.flush(); 30 buf.close(); 31 long end2 = System.currentTimeMillis(); 32 BufferedOutputStreamTime += end2 - begin2; 33 34 //Test FileWriter 35 long begin3 = System.currentTimeMillis(); 36 // the second parameter "true",Whether or not a file will be covered 37 // or appended. 38 fw = new FileWriter(f); 39 // fw = new FileWriter("d:/test/testwrite/add2.txt",true); 40 fw.write(content); 41 fw.close(); 42 long end3 = System.currentTimeMillis(); 43 FileWriterTime += end3 - begin3; 44 } catch (Exception e) { 45 e.printStackTrace(); 46 } finally { 47 try { 48 fw.close(); 49 buf.close(); 50 outStr.close(); 51 out.close(); 52 } catch (Exception e) { 53 e.printStackTrace(); 54 } 55 } 56 } 57 58 public static void read(String filePath){ 59 FileInputStream in = null; 60 BufferedInputStream buf = null; 61 FileReader reader = null; 62 BufferedReader br = null; 63 StringBuffer sb = new StringBuffer(); 64 65 try { 66 //Test FileInputStream 67 long begin1 = System.currentTimeMillis(); 68 File f = new File(filePath); 69 in = new FileInputStream(f); 70 int len1 = 512; 71 byte[] bytes1 = new byte[len1]; 72 73 while ((len1 = in.read(bytes1, 0, len1)) !62616964757a686964616fe59b9ee7ad9431333366303138= -1) { 74 if(len1 < 512){ 75 byte[] tmpBuf = new byte[len1]; 76 System.arraycopy(bytes1, 0, tmpBuf, 0, len1);

77 sb.append(new String(tmpBuf)); 78 tmpBuf = null; 79 }else{ 80 sb.append(new String(bytes1)); 81 } 82 } 83 84 in.close(); 85 long end1 = System.currentTimeMillis(); 86 FileInputStreamTime += end1 - begin1; 87 88 //Test BufferedInputStream 89 long begin2 = System.currentTimeMillis(); 90 int len2 = 512; 91 byte[] bytes2 = new byte[len2]; 92 buf = new BufferedInputStream(new FileInputStream(f)); 93 while ((len2 = buf.read(bytes2, 0, len2)) != -1) { 94 if(len2 < 512){ 95 byte[] tmpBuf = new byte[len2]; 96 System.arraycopy(bytes2, 0, tmpBuf, 0, len2);

97 sb.append(new String(tmpBuf)); 98 tmpBuf = null; 99 }else{100 sb.append(new String(bytes2));101 }102 }103 104 buf.close();105 long end2 = System.currentTimeMillis();106 BufferedInputStreamTime += end2 - begin2;107 108 //Test FileReader109 long begin3 = System.currentTimeMillis();110 reader = new FileReader(f);111 br = new BufferedReader(reader);112 String str;113 while ((str = br.readLine()) != null) {114 sb.append(str);115 }116 br.close();117 reader.close();118 long end3 = System.currentTimeMillis();119 FileReaderTime += end3 - begin3;120 121 } catch (Exception e) {122 e.printStackTrace();123 } finally {124 try {125 br.close();126 reader.close();127 in.close();128 buf.close();129 } catch (Exception e) {130 e.printStackTrace();131 }132 }133 }

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值