JAVA写文件

最近对以前开发的一个通用数据迁移的软件进行优化。除了用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>

  1. imp<wbr>ort<wbr>java.io.File; <wbr><wbr></wbr></wbr></wbr></wbr>
  2. imp<wbr>ort<wbr>java.io.FileOutputStream; <wbr><wbr></wbr></wbr></wbr></wbr>
  3. imp<wbr>ort<wbr>java.io.*; <wbr><wbr></wbr></wbr></wbr></wbr>
  4. <wbr><wbr></wbr></wbr>
  5. public<wbr>class<wbr>FileTest<wbr>{ <wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  6. <wbr><wbr><wbr><wbr>public<wbr>FileTest()<wbr>{ <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  7. <wbr><wbr><wbr><wbr>} <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  8. <wbr><wbr></wbr></wbr>
  9. <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>
  10. <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>
  11. <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>
  12. <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>
  13. <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>
  14. <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>
  15. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>try<wbr>{ <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  16. <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>
  17. <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>
  18. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>for<wbr>(int<wbr>i<wbr>=<wbr>0;<wbr>i<wbr>&lt;<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>
  19. <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>
  20. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  21. <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>
  22. <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>
  23. <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>
  24. <wbr><wbr></wbr></wbr>
  25. <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>
  26. <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>
  27. <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>
  28. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>for<wbr>(int<wbr>i<wbr>=<wbr>0;<wbr>i<wbr>&lt;<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>
  29. <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>
  30. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  31. <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>
  32. <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>
  33. <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>
  34. <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>
  35. <wbr><wbr></wbr></wbr>
  36. <wbr><wbr></wbr></wbr>
  37. <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>
  38. <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>
  39. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>for<wbr>(int<wbr>i<wbr>=<wbr>0;<wbr>i<wbr>&lt;<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>
  40. <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>
  41. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  42. <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>
  43. <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>
  44. <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>
  45. <wbr><wbr></wbr></wbr>
  46. <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>
  47. <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>
  48. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  49. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>finally<wbr>{ <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  50. <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>
  51. <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>
  52. <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>
  53. <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>
  54. <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>
  55. <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>
  56. <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>
  57. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  58. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  59. <wbr><wbr><wbr><wbr>} <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  60. <wbr><wbr></wbr></wbr>
  61. <wbr><wbr></wbr></wbr>
  62. }<wbr><wbr><wbr></wbr></wbr></wbr>

<wbr></wbr>

  1. 1.当count=1000的,即写文件1000行的时候,写出的文件大小为18.5KB: <wbr><wbr></wbr></wbr>
  2. FileOutputStream执行耗时:46<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
  3. BufferedOutputStream执行耗时:31<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
  4. FileWriter执行耗时:15<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
  5. <wbr><wbr></wbr></wbr>
  6. <wbr><wbr></wbr></wbr>
  7. 2.当count=10000的,即写文件10000行的时候,写出的文件大小为185KB: <wbr><wbr></wbr></wbr>
  8. FileOutputStream执行耗时:188<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
  9. BufferedOutputStream执行耗时:32<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
  10. FileWriter执行耗时:16<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
  11. <wbr><wbr></wbr></wbr>
  12. <wbr><wbr><wbr></wbr></wbr></wbr>
  13. <wbr><wbr></wbr></wbr>
  14. 3.当count=100000的,即写文件100000行的时候,写出的文件大小为1856KB: <wbr><wbr></wbr></wbr>
  15. FileOutputStream执行耗时:1266<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
  16. BufferedOutputStream执行耗时:125<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
  17. FileWriter执行耗时:93<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
  18. <wbr><wbr></wbr></wbr>
  19. <wbr><wbr><wbr></wbr></wbr></wbr>
  20. <wbr><wbr></wbr></wbr>
  21. 4.当count=1000000的,即写文件1000000行的时候,写出的文件大小为18555KB: <wbr><wbr></wbr></wbr>
  22. FileOutputStream执行耗时:12063<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
  23. BufferedOutputStream执行耗时:1484<wbr>豪秒 <wbr><wbr></wbr></wbr></wbr>
  24. 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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值