[测试]javaIO写入文件效率——几种方法比较

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

各类写入方法

/***1 按字节写入 FileOutputStream

*

*@paramcount 写入循环次数

*@paramstr 写入字符串*/publicvoidoutputStreamTest(intcount, String str) {

File f=newFile("f:test1.txt");

OutputStream os=null;try{

os=newFileOutputStream(f);for(inti = 0; i < count; i++) {

os.write(str.getBytes());

}

os.flush();

System.out.PRintln("file's long:" +f.length());

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{try{

os.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}/***2 按字节缓冲写入 BufferedOutputStream

*

*@paramcount 写入循环次数

*@paramstr 写入字符串*/publicvoidbufferedOutputTest(intcount, String str) {

File f=newFile("f:test2.txt");

BufferedOutputStream bos=null;try{

OutputStream os=newFileOutputStream(f);

bos=newBufferedOutputStream(os);for(inti = 0; i < count; i++) {

bos.write(str.getBytes());

}

bos.flush();

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{try{

bos.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}/***3 按字符写入 FileWriter

*

*@paramcount 写入循环次数

*@paramstr 写入字符串*/publicvoidfileWriteTest(intcount, String str) {

File f=newFile("f:test.txt");

Writer writer=null;try{

writer=newFileWriter(f);for(inti = 0; i < count; i++) {

writer.write(str);

}

writer.flush();

}catch(IOException e) {

e.printStackTrace();

}finally{try{

writer.close();

}catch(Exception e) {

e.printStackTrace();

}

}

}/***4 按字符缓冲写入 BufferedWriter

*

*@paramcount 写入循环次数

*@paramstr 写入字符串*/publicvoidbufferedWriteTest(intcount, String str) {

File f=newFile("f:test3.txt");

OutputStreamWriter writer=null;

BufferedWriter bw=null;try{

OutputStream os=newFileOutputStream(f);

writer=newOutputStreamWriter(os);

bw=newBufferedWriter(writer);for(inti = 0; i < count; i++) {

bw.write(str);

}

bw.flush();if(f.exists()){

f.delete();

}

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{try{

bw.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}/***5 按字符缓冲写入 BufferedWriter and BufferedOutputStream

*

*@paramcount 写入循环次数

*@paramstr 写入字符串*/publicvoidbufferedWriteAndBufferedOutputStreamTest(intcount, String str) {

File f=newFile("f:test4.txt");

BufferedOutputStream bos=null;

OutputStreamWriter writer=null;

BufferedWriter bw=null;try{

OutputStream os=newFileOutputStream(f);

bos=newBufferedOutputStream(os);

writer=newOutputStreamWriter(bos);

bw=newBufferedWriter(writer);for(inti = 0; i < count; i++) {

bw.write(str);

}

bw.flush();if(f.exists()){

f.delete();

System.out.println("delete---");

}

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{try{

bw.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}/***6 按字符缓冲写入 BufferedWriter and FileWriter

*

*@paramcount 写入循环次数

*@paramstr 写入字符串*/publicvoidbufferedWriteAndFileWriterTest(intcount, String str) {

File f=newFile("f:test5.txt");

FileWriter fw=null;

BufferedWriter bw=null;try{

fw=newFileWriter(f);

bw=newBufferedWriter(fw);for(inti = 0; i < count; i++) {

bw.write(str);

}

bw.flush();

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{try{

bw.close();if(f.exists()){

f.delete();

}

}catch(IOException e) {

e.printStackTrace();

}

}

}

测试写入类

publicstaticvoidmain(String[] args) {

String str= "abcdefghiJKLMN!";intcount = 1000000;

TestOutputStream t=newTestOutputStream();//1.fileWrite's timelongstart =System.currentTimeMillis();

t.fileWriteTest(count, str);longend =System.currentTimeMillis();

System.out.println("fileWrite's time---------" + (start -end));//2.outputStreamTest's timestart =System.currentTimeMillis();

t.outputStreamTest(count, str);

end=System.currentTimeMillis();

System.out.println("outputStreamTest's time---------" + (start -end));//3.bufferedOutputTest's timestart =System.currentTimeMillis();

t.bufferedOutputTest(count, str);

end=System.currentTimeMillis();

System.out.println("bufferedOutputTest's time---------" + (start -end));//4.bufferedWriteTest's timestart =System.currentTimeMillis();

t.bufferedWriteTest(count, str);

end=System.currentTimeMillis();

System.out.println("bufferedWriteTest's time---------" + (start -end));//5.bufferedWrite And FileWriterTest's timestart =System.currentTimeMillis();

t.bufferedWriteAndFileWriterTest(count, str);

end=System.currentTimeMillis();

System.out.println("bufferedWrite And FileWriterTest's time---------" + (start -end));//6.bufferedWrite And BufferedOutputStreamTest's timestart =System.currentTimeMillis();

t.bufferedWriteAndBufferedOutputStreamTest(count, str);

end=System.currentTimeMillis();

System.out.println("bufferedWrite And BufferedOutputStreamTest's time---------" + (start -end));

}

测试结果

/*** 测试结果

*

* 1.file's long:16kb

*

fileWrite's time----------36

outputStreamTest's time----------167

bufferedOutputTest's time----------17

bufferedWriteTest's time----------14

bufferedWrite And FileWriterTest's time----------9

bufferedWrite And BufferedOutputStreamTest's time----------12

*

* 2.file's long:1600kb

*

fileWrite's time----------69

outputStreamTest's time----------1282

bufferedOutputTest's time----------68

bufferedWriteTest's time----------40

bufferedWrite And FileWriterTest's time----------52

bufferedWrite And BufferedOutputStreamTest's time----------37

*

* 3.file's long:16000kb

*

fileWrite's time----------555

outputStreamTest's time----------12448

bufferedOutputTest's time----------599

bufferedWriteTest's time----------346

bufferedWrite And FileWriterTest's time----------316

bufferedWrite And BufferedOutputStreamTest's time----------358

*

*4.file's long:160000kb

*

fileWrite's time----------5203

outputStreamTest's time----------127182

bufferedOutputTest's time----------5972

bufferedWriteTest's time----------3445 最优

bufferedWrite And FileWriterTest's time----------5904

bufferedWrite And BufferedOutputStreamTest's time----------5353

*

*5.file's long:1600000kb

*

fileWrite's time----------50416

outputStreamTest's time----------1303242

bufferedOutputTest's time----------60931

bufferedWriteTest's time----------46697

bufferedWrite And FileWriterTest's time----------48710

bufferedWrite And BufferedOutputStreamTest's time----------64354*/

总结:

如果按字符和字节来分类,除方法1和2,其余都是按字符写入文件,字符写入一般比字节快;看javaAPI可知,FileWriter的父类就是OutputStreamWriter,他俩都是实现Writer类,从这点上来说,方法4和6几乎没区别,时间有些微的差别,但内部机制是一样的。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航