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

    添加时间:2013-5-14 点击量:

      迩来,在做办事器后台处理惩罚数据的时辰,须要用到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 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 FileReader
    109 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 }


      测试时,分别对不合大小的数据做500次同样的操纵,取得的均匀耗时如下:





    不合流耗时对比(ms)
     我所有的自负皆来自我的自卑,所有的英雄气概都来自于我的软弱。嘴里振振有词是因为心里满是怀疑,深情是因为痛恨自己无情。这世界没有一件事情是虚空而生的,站在光里,背后就会有阴影,这深夜里一片寂静,是因为你还没有听见声音。—— 马良《坦白书》
    分享到: