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/# ">
- ImportJava. Io .*;
- /*************************************** ****************************************
- *
- * @ Author pengcqu
- *
- */
- Public ClassTestbuffer {
- Public Static VoidMain (string ARGs [])ThrowsIoexception {
- Testbuffer BR =NewTestbuffer ();
- String from = "D:/A/1. MP3 ";
- LongStarttime = system. currenttimemillis ();
- BR. readwrite (from, "d:/B/2. MP3 ");
- LongEndtime = system. currenttimemillis ();
- System. Out. println ("direct read time:" + (endtime-starttime) + "Ms ");
- LongStarttime1 = system. currenttimemillis ();
- BR. readwritewithbuffer (from, "d:/B/3. MP3 ");
- LongEndtime1 = system. currenttimemillis ();
- System. Out. println ("Time consumed to read using the buffer:" + (endtime1-starttime1) + "Ms ");
- }
- /*************************************** ************************************
- * Directly read files
- *
- * @ Param from
- * @ Param
- * @ Throws ioexception
- */
- Public Static VoidReadwrite (string from, string)ThrowsIoexception {
- Inputstream in =Null;
- Outputstream out =Null;
- Try{
- In =NewFileinputstream (from );
- Out =NewFileoutputstream ();
- While(True){
- IntData = in. Read ();
- If(Data =-1 ){
- Break;
- }
- Out. Write (data );
- }
- }Finally{
- If(In! =Null){
- In. Close ();
- }
- If(Out! =Null){
- Out. Close ();
- }
- }
- }
- /*************************************** ************************************
- * Use the cache area to read and write files
- * @ Param from
- * @ Param
- * @ Throws ioexception
- */
- Public Static VoidReadwritewithbuffer (string from, string)
- ThrowsIoexception {
- Inputstream inbuffer =Null;
- Outputstream outbuffer =Null;
- Try{
- Inbuffer =NewBufferedinputstream (NewFileinputstream (from ));
- Outbuffer =NewBufferedoutputstream (NewFileoutputstream ());
- While(True){
- IntData = inbuffer. Read ();
- If(Data =-1 ){
- Break;
- }
- Outbuffer. Write (data );
- }
- }Finally{
- If(Inbuffer! =Null){
- Inbuffer. Close ();
- }
- If(Outbuffer! =Null){
- Outbuffer. Close ();
- }
- }
- }
- }
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.