最近对以前开发的一个通用数据迁移的软件进行优化。除了用JDK5.0的多线程技术进行的改进之外,也比较了一下java写文件的性能。<wbr><br><wbr><wbr><wbr>在 java写文件中,通常会使用FileOutputStream和FileWriter,FileWriter只能写文本文件。 FileOutputStream也经常结合BufferedOutputStream。因为实际应用中写文本文件的情况占了大多数。所以下面测试用不同 的方式生成一个相同行数、大小相同的文件的三种不同方式。<wbr><br></wbr></wbr></wbr></wbr></wbr>
Java代码<wbr><a href="http://zhangzcz1999.javaeye.com/blog/515121#" style="text-decoration:none; color:rgb(8,68,135)"><img alt="java写文件比较 - qunqun - 群群的梦想" border="0" src="http://zhangzcz1999.javaeye.com/images/icon_copy.gif" title="java写文件比较" style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; border-style:initial; border-color:initial; max-width:100%"></a></wbr>
<wbr></wbr>
- imp<wbr>ort<wbr>java.io.File; <wbr><wbr></wbr></wbr></wbr></wbr>
- imp<wbr>ort<wbr>java.io.FileOutputStream; <wbr><wbr></wbr></wbr></wbr></wbr>
- imp<wbr>ort<wbr>java.io.*; <wbr><wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr></wbr></wbr>
- public<wbr>class<wbr>FileTest<wbr>{ <wbr><wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr>public<wbr>FileTest()<wbr>{ <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr>} <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr>public<wbr>static<wbr>void<wbr>main(String[]<wbr>args)<wbr>{ <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>FileOutputStream<wbr>out<wbr>=<wbr>null; <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>FileOutputStream<wbr>outSTr<wbr>=<wbr>null; <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>BufferedOutputStream<wbr>Buff=null; <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>FileWriter<wbr>fw<wbr>=<wbr>null; <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>int<wbr>count=1000;//写文件行数 <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>try<wbr>{ <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>out<wbr>=<wbr>new<wbr>FileOutputStream(new<wbr>File("C:/add.txt")); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>long<wbr>begin<wbr>=<wbr>System.currentTimeMillis(); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>for<wbr>(int<wbr>i<wbr>=<wbr>0;<wbr>i<wbr><<wbr>count;<wbr>i++)<wbr>{ <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>out.write("测试java<wbr>文件操作\r\n".getBytes()); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>out.close(); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>long<wbr>end<wbr>=<wbr>System.currentTimeMillis(); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println("FileOutputStream执行耗时:"<wbr>+<wbr>(end<wbr>-<wbr>begin)<wbr>+<wbr>"<wbr>豪秒"); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>outSTr<wbr>=<wbr>new<wbr>FileOutputStream(new<wbr>File("C:/add0.txt")); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Buff=new<wbr>BufferedOutputStream(outSTr); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>long<wbr>begin0<wbr>=<wbr>System.currentTimeMillis(); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>for<wbr>(int<wbr>i<wbr>=<wbr>0;<wbr>i<wbr><<wbr>count;<wbr>i++)<wbr>{ <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Buff.write("测试java<wbr>文件操作\r\n".getBytes()); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Buff.flush(); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Buff.close(); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>long<wbr>end0<wbr>=<wbr>System.currentTimeMillis(); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println("BufferedOutputStream执行耗时:"<wbr>+<wbr>(end0<wbr>-<wbr>begin0)<wbr>+<wbr>"<wbr>豪秒"); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr></wbr></wbr>
- <wbr><wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>fw<wbr>=<wbr>new<wbr>FileWriter("C:/add2.txt"); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>long<wbr>begin3<wbr>=<wbr>System.currentTimeMillis(); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>for<wbr>(int<wbr>i<wbr>=<wbr>0;<wbr>i<wbr><<wbr>count;<wbr>i++)<wbr>{ <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>fw.write("测试java<wbr>文件操作\r\n"); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>fw.close(); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>long<wbr>end3<wbr>=<wbr>System.currentTimeMillis(); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println("FileWriter执行耗时:"<wbr>+<wbr>(end3<wbr>-<wbr>begin3)<wbr>+<wbr>"<wbr>豪秒"); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<wbr>catch<wbr>(Exception<wbr>e)<wbr>{ <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>e.printStackTrace(); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>finally<wbr>{ <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>try<wbr>{ <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>fw.close(); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Buff.close(); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>outSTr.close(); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>out.close(); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<wbr>catch<wbr>(Exception<wbr>e)<wbr>{ <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>e.printStackTrace(); <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr>} <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr></wbr></wbr>
- <wbr><wbr></wbr></wbr>
- }<wbr><wbr><wbr></wbr></wbr></wbr>
<wbr></wbr>
- 1.当count=1000的,即写文件1000行的时候,写出的文件大小为18.5KB: <wbr><wbr></wbr></wbr>
- FileOutputStream执行耗时:46<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
- BufferedOutputStream执行耗时:31<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
- FileWriter执行耗时:15<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
- <wbr><wbr></wbr></wbr>
- <wbr><wbr></wbr></wbr>
- 2.当count=10000的,即写文件10000行的时候,写出的文件大小为185KB: <wbr><wbr></wbr></wbr>
- FileOutputStream执行耗时:188<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
- BufferedOutputStream执行耗时:32<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
- FileWriter执行耗时:16<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
- <wbr><wbr></wbr></wbr>
- <wbr><wbr><wbr></wbr></wbr></wbr>
- <wbr><wbr></wbr></wbr>
- 3.当count=100000的,即写文件100000行的时候,写出的文件大小为1856KB: <wbr><wbr></wbr></wbr>
- FileOutputStream执行耗时:1266<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
- BufferedOutputStream执行耗时:125<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
- FileWriter执行耗时:93<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
- <wbr><wbr></wbr></wbr>
- <wbr><wbr><wbr></wbr></wbr></wbr>
- <wbr><wbr></wbr></wbr>
- 4.当count=1000000的,即写文件1000000行的时候,写出的文件大小为18555KB: <wbr><wbr></wbr></wbr>
- FileOutputStream执行耗时:12063<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
- BufferedOutputStream执行耗时:1484<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
- FileWriter执行耗时:969<wbr>豪秒<wbr><wbr></wbr></wbr></wbr>
由 以上数据可以看到,如果不用缓冲流BufferedOutputStream,FileOutputStream写文件的鲁棒性是很不好的。当写 1000000行的文件的时候,FileOutputStream比FileWriter要慢11094毫秒(11 秒),BufferedOutputStream比FileWriter慢515毫秒。<wbr><br><wbr><wbr><wbr>不要小看这几秒的时间。当操作的数据量很大的时候,这点性能的差距就会很大了。在通用数据迁移工具导出数据库2千万条记录生成sql脚本文件的时候,性能性能相差10分钟以上。</wbr></wbr></wbr></wbr>