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

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

FileOutputStreamTime = 0;BufferedOutputStreamTime = 0;FileWriterTime = 0;FileInputStreamTime = 0;BufferedInputStreamTime = 0;FileReaderTime = 0; write(String filePath, String content){ 9FileOutputStream out = null; 10FileOutputStream outStr = null; 11BufferedOutputStream buf = null; 12FileWriter fw = null; 13File f = new File(filePath); {begin1 = System.currentTimeMillis(); 18out = new FileOutputStream(f); 19 out.write(content.getBytes()); 20 out.close(); 21long end1 = System.currentTimeMillis(); 22FileOutputStreamTime += end1 – begin1;begin2 = System.currentTimeMillis(); 26outStr = new FileOutputStream(f); 27buf = new BufferedOutputStream(outStr); 28 buf.write(content.getBytes()); 29 buf.flush(); 30 buf.close(); 31long end2 = System.currentTimeMillis(); 32BufferedOutputStreamTime += end2 – begin2;begin3 = System.currentTimeMillis(); 36// the second parameter “true”,Whether or not a file will be coveredfw = new FileWriter(f);fw.write(content); 41 fw.close(); 42long end3 = System.currentTimeMillis(); 43FileWriterTime += end3 – begin3; 44} catch (Exception e) { 45 e.printStackTrace(); 46} finally { 47try { 48 fw.close(); 49 buf.close(); 50 outStr.close(); 51 out.close(); 52} catch (Exception e) { 53 e.printStackTrace(); 54 } 55 } 56 } read(String filePath){ 59FileInputStream in = null; 60BufferedInputStream buf = null; 61FileReader reader = null; 62BufferedReader br = null; 63StringBuffer sb = new StringBuffer(); {begin1 = System.currentTimeMillis(); 68File f = new File(filePath); 69in = new FileInputStream(f); 70int len1 = 512;[len1];((len1 = in.read(bytes1, 0, len1)) != -1) { 74if(len1 < 512){[len1]; 76System.arraycopy(bytes1, 0, tmpBuf, 0, len1); 77sb.append(new String(tmpBuf)); 78tmpBuf = null; 79}else{ 80sb.append(new String(bytes1)); 81 } 82 } 83 84 in.close(); 85long end1 = System.currentTimeMillis(); 86FileInputStreamTime += end1 – begin1;begin2 = System.currentTimeMillis(); 90int len2 = 512;[len2]; 92buf = new BufferedInputStream(new FileInputStream(f)); 93while ((len2 = buf.read(bytes2, 0, len2)) != -1) { 94if(len2 < 512){[len2]; 96System.arraycopy(bytes2, 0, tmpBuf, 0, len2); 97sb.append(new String(tmpBuf)); 98tmpBuf = null; 99}else{100sb.append(new String(bytes2));101 }102 }103 104 buf.close();105long end2 = System.currentTimeMillis();106BufferedInputStreamTime += end2 – begin2;begin3 = System.currentTimeMillis();110reader = new FileReader(f);111br = new BufferedReader(reader);112 String str;113while ((str = br.readLine()) != null) {114 sb.append(str);115 }116 br.close();117 reader.close();118long end3 = System.currentTimeMillis();119FileReaderTime += end3 – begin3;120 121} catch (Exception e) {122 e.printStackTrace();123} finally {124try {125 br.close();126 reader.close();127 in.close();128 buf.close();129} catch (Exception e) {130 e.printStackTrace();131 }132 }133}造物之前,必先造人。

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

相关文章:

你感兴趣的文章:

标签云: