Improve I/O performance through the buffer in Java

Source: Internet
Author: User

We know that the input and output stream classes in Java use a single-byte read method to perform I/O operations. That is to say, each read-only write of one byte of data, this method is obviously cumbersome and inefficient. If you read 10 MB of files from a device and read one byte each time, you will need to perform 10 MB of I/O operations. I/O operations are quite time-consuming, undoubtedly, the system performance is greatly reduced.

 

 

Java code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://writeblog.csdn.net/# ">
  1. ImportJava. Io .*;
  2. /*************************************** ****************************************
  3. *
  4. * @ Author pengcqu
  5. *
  6. */
  7. Public ClassTestbuffer {
  8. Public Static VoidMain (string ARGs [])ThrowsIoexception {
  9. Testbuffer BR =NewTestbuffer ();
  10. String from = "D:/A/1. MP3 ";
  11. LongStarttime = system. currenttimemillis ();
  12. BR. readwrite (from, "d:/B/2. MP3 ");
  13. LongEndtime = system. currenttimemillis ();
  14. System. Out. println ("direct read time:" + (endtime-starttime) + "Ms ");
  15. LongStarttime1 = system. currenttimemillis ();
  16. BR. readwritewithbuffer (from, "d:/B/3. MP3 ");
  17. LongEndtime1 = system. currenttimemillis ();
  18. System. Out. println ("Time consumed to read using the buffer:" + (endtime1-starttime1) + "Ms ");
  19. }
  20. /*************************************** ************************************
  21. * Directly read files
  22. *
  23. * @ Param from
  24. * @ Param
  25. * @ Throws ioexception
  26. */
  27. Public Static VoidReadwrite (string from, string)ThrowsIoexception {
  28. Inputstream in =Null;
  29. Outputstream out =Null;
  30. Try{
  31. In =NewFileinputstream (from );
  32. Out =NewFileoutputstream ();
  33. While(True){
  34. IntData = in. Read ();
  35. If(Data =-1 ){
  36. Break;
  37. }
  38. Out. Write (data );
  39. }
  40. }Finally{
  41. If(In! =Null){
  42. In. Close ();
  43. }
  44. If(Out! =Null){
  45. Out. Close ();
  46. }
  47. }
  48. }
  49. /*************************************** ************************************
  50. * Use the cache area to read and write files
  51. * @ Param from
  52. * @ Param
  53. * @ Throws ioexception
  54. */
  55. Public Static VoidReadwritewithbuffer (string from, string)
  56. ThrowsIoexception {
  57. Inputstream inbuffer =Null;
  58. Outputstream outbuffer =Null;
  59. Try{
  60. Inbuffer =NewBufferedinputstream (NewFileinputstream (from ));
  61. Outbuffer =NewBufferedoutputstream (NewFileoutputstream ());
  62. While(True){
  63. IntData = inbuffer. Read ();
  64. If(Data =-1 ){
  65. Break;
  66. }
  67. Outbuffer. Write (data );
  68. }
  69. }Finally{
  70. If(Inbuffer! =Null){
  71. Inbuffer. Close ();
  72. }
  73. If(Outbuffer! =Null){
  74. Outbuffer. Close ();
  75. }
  76. }
  77. }
  78. }
Import Java. io. *; /*************************************** **************************************** ** @ author pengcqu **/public class testbuffer {public static void main (string ARGs []) throws ioexception {testbuffer BR = new testbuffer (); string from = "D:/A/1. MP3 "; long starttime = system. currenttimemillis (); BR. readwrite (from, "d:/B/2. MP3 "); long endtime = system. currenttimemillis (); system. out. printl N ("direct read time:" + (endtime-starttime) + "Ms"); long starttime1 = system. currenttimemillis (); BR. readwritewithbuffer (from, "d:/B/3. MP3 "); long endtime1 = system. currenttimemillis (); system. out. println ("buffer reading time consumed:" + (endtime1-starttime1) + "Ms ");} /*************************************** * ************************************ directly read the file ** @ Param from * @ Param to * @ throws ioexception */public static void rea Dwrite (string from, string to) throws ioexception {inputstream in = NULL; outputstream out = NULL; try {In = new fileinputstream (from); out = new fileoutputstream (); while (true) {int DATA = in. read (); If (Data =-1) {break;} Out. write (data) ;}} finally {If (in! = NULL) {in. Close () ;}if (OUT! = NULL) {out. close ();}}} /*************************************** * ************************************ use the cache Zone read/write files * @ Param from * @ Param to * @ throws ioexception */public static void readwritewithbuffer (string from, string to) throws ioexception {inputstream inbuffer = NULL; outputstream outbuffer = NULL; try {inbuffer = new bufferedinputstream (New fileinputstream (from); outbuffer = new bufferedoutputs Tream (New fileoutputstream (to); While (true) {int DATA = inbuffer. read (); If (Data =-1) {break;} outbuffer. write (data) ;}} finally {If (inbuffer! = NULL) {inbuffer. Close ();} If (outbuffer! = NULL) {outbuffer. Close ();}}}}

Result: for a file of the size of 39266 MB, the direct read/write time is 719 ms, and the buffer read/write time is ms. It can be seen that the performance of reading and writing files through buffer is significantly better than that of direct reading and writing.

Java provides a buffer class to improve the I/O efficiency. This is like a temporary buffer for reading and writing data in a buffer every time and writing the database to the target device at a time. There are two reading methods.

 


For example, there are 10000 books in a that need to be moved to B. If one book is moved at a time, it will take 10000 times. If you place 1000 yuan each time on a freight car and ship it to location B, it takes 10 times to complete. The freight car is equivalent to the cache zone. Similarly, opening a data cache area to read data blocks at a time significantly improves reading efficiency. The following uses a specific code example to show the performance differences between the two.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.