永发信息网

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

答案:1  悬赏:20  手机版
解决时间 2021-04-06 02:25
几种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)) != -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 }
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
崩坏3商店买女武神碎片要多少水晶
煲草鱼头汤能放当归吗?
张葛在哪里啊,我有事要去这个地方
前端总线频率5.0GT/s换算成多少MHz?
11300.60大写如何写
输入阻抗和输出阻抗对电路有什么意义?比如他
假如只接火线,不接零线的情况下,火线漏电或
---WhenisMaryleaving?---Sheisdueto______at
中邮速递易的小黄筒试点目前只在上海吗?
西安剪短发是发新社好还是X21,希望有亲身体
歌曲《致战友》曲中那段纯音乐是什么乐器演奏
五一的祝福,谁给我发一条五一祝福的好短信,
生物体进行生命活动的基础是A.具有共同的物质
冰河时期是什么时候?
使劲的劲要怎么组词
推荐资讯
单选题运动员在鞍马上作腾空的翻滚动作时,下
新疆采棉机 手持式采棉机哪里有卖的求解答
—Look, there many birds around here, but
电话offer会变卦吗,已经定薪和入职时间
怎样才会被认定是恶意透支
潮阳晚上有公交吗
分析聂赫留朵夫这一“忏悔的贵族”的形象
一辆小车最多可开多少年
怎样开个电瓶加盟店
身上软没力气还着急烦燥想发火,有时候像有鬼
吃吃的爱台词有的人,《诗经》比拟的诗句有哪
翻译 谈之以书责杰,长辑径归
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?