Java IO streams Comments (five) - buffered stream

    Buffer also known efficient flow stream, a process stream, that is acting on the flow stream. Its purpose is to accelerate the speed of reading and writing data.

    IO buffer stream itself does not function, but the buffering effect thereby increasing the efficiency plus in other streams. When a file or other objects frequently read and write operations, or inefficient, performance difference. At this time the buffer flow can be used more efficiently read and write information. Because the first buffered stream cached data, and writing or reading out together. Therefore, the buffer flow is still very important, plus the buffer flow to enhance performance when operating in IO.


    Java IO stream corresponding to the stream buffer has the following four:

    Byte stream buffer: BufferedInputStream, BufferedOutputStream

    Character buffer stream: BufferedReader, BufferedWriter


1, the buffer byte stream

Construction method:

   Input stream:

  • BufferedInputStream (InputStream in): Creates a new byte buffer input stream, the incoming parameter is the InputStream type, the default buffer size is 8129.
  • BufferedInputStream (InputStream in, int size): Create a byte buffer size specified input stream buffer.

   Output stream:

  • BufferedOutputStream (OutputStream out): Create a new byte buffered output stream, the parameter is passed in the OutputStream, to write data to the specified output stream basis.
  • BufferedOutputStream (OutputStream out, int size): Create a specified buffer size byte buffered output stream, the data is written with the specified buffer size specified output stream basis.

   Examples constructor code is as follows:

    // byte buffered input and output streams, this method default buffer size 8192 
    BufferedInputStream BIS = new new BufferedInputStream ( new new the FileInputStream ( " D: \\ \\ hello.txt the IO ")); 
    BufferedOutputStream The BOS = new new BufferedOutputStream The ( new new a FileOutputStream ( " D: \\ \\ hello.txt the IO "));
     // custom buffer size 
    BufferedInputStream BIS1 = new new BufferedInputStream ( new new the FileInputStream ( " D: \\ \\ hello.txt the IO "), 10240); 
    BufferedOutputStream The bos1 = new new BufferedOutputStream The ( new new a FileOutputStream ( " D: \\ \\ hello.txt the IO "), 10240);

   

    Said front buffer stream can accelerate the speed of read and write data, so now used to compare the efficiency of the normal stream and the buffer stream contrast (a 886MB sized video copy):

    Normal stream Code Example:

package com.thr;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author Administrator
 * @date 2020-02-26
 * @desc 普通流测试
 */
public class BufferedDemo {
    public static void main(String[] args) {
        //定义流
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //开始时间
            long= System.currentTimeMillis Start ();
             // Create a stream object 
            FIS = new new FileInputStream ( " D: \\ \\ 1.mp4 IO "); 
            fos = new new FileOutputStream ( " D: \\ \\ 2.mp4 IO ") ;
             // read and write operations 
            int len;
             byte [] Buffer = new new  byte [1024];
             the while ((len = fis.read (Buffer)) = -. 1!) { 
                fos.write (Buffer, 0, len); 
            } // end time Long end = System.currentTimeMillis (); 
            System.out.println ( " completed total time: " + (Start-end) + " MS
            
            ");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fos.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
//完成,共耗时:5165ms

    Buffer Flow Code Example:

Package com.thr; Import the java.io. *; / ** 
 * @author Administrator 
 * @date 2020-02-26 
 * @desc test stream buffer 
 * / public class BufferedDemo1 {
     public static void main (String [] args) {
         // define the buffer flow 
        BufferedInputStream BIS = null ; 
        BufferedOutputStream the BOS = null ;
         the try {
             // start time Long start = System.currentTimeMillis ();
             // create a buffered stream objects, note FileXXX parameters passed without file directory 
            BIS = new new BufferedInputStream ( new new




   
             FileInputStream("D:\\IO\\1.mp4"));
            bos = new BufferedOutputStream(new FileOutputStream("D:\\IO\\3.mp4"));
            //读写操作
            int len;
            byte[] buffer = new byte[1024];
            while ((len=bis.read(buffer))!=-1){
                bos.write(buffer,0,len);
            }
            //结束时间
            long end = System.currentTimeMillis();
            System.out.println("完成,共耗时:"+(end-start)+"ms");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bos.close();
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
//完成,共耗时:1658ms

    We can see that the buffer flow is probably only a third of the time to complete the same job.


2, the character stream buffer

Construction method

    Input stream:

  • BufferedReader (Reader in): Creates a new character buffer input stream, the incoming parameter is the type Reader, the default buffer size is 8129.
  • BufferedReader (Reader in, int sz): create a buffer size of the buffer specified character input stream.

    Output stream:

  • BufferedWriter (Writer out): Creates a new character buffer the output stream, the incoming parameter is the type Writer, the default buffer size is 8129.
  • BufferedWriter (Writer out, int sz): Create a specified character size of the buffer buffered output stream.

  Examples constructor code is as follows:

    // character buffer input and output streams, this method default buffer size is 8192 = defaultCharBufferSize; 
    the BufferedReader br = new new the BufferedReader ( new new (the FileReader " D: \\ \\ hello.txt the IO ")); 
    BufferedWriter, BW = new new BufferedWriter, ( new new FileWriter ( " D: \\ \\ hello.txt the IO "));
     // buffer size to customize 
    the BufferedReader BR1 = new new the BufferedReader ( new new the FileReader ( " D: \\ \\ hello.txt the IO "), 10240) ; 
    BufferedWriter, BW1 = new new BufferedWriter, ( new new FileWriter ( " D: \\ \\ hello.txt the IO "), 10240);

Byte character buffer and the buffer flow stream using substantially the same process but not the same thing both. However, the buffer in the character stream that has two distinct methods.

  • BufferedReader: public String readLine (): Read one row of data. Read the last return null.
  • BufferedWriter: public void newLine (): wrap the interior of the method calls lineSeparator, it represents a newline.

Examples using two methods:

package com.thr;

import java.io.*;

/**
 * @author Administrator
 * @date 2020-02-26
 * @desc ReadLine和newLine的使用
 */
public class BufferedTest {
    public static void main(String[] args) {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {

            br = new BufferedReader(new FileReader("D:\\IO\\hello.txt"));
            bw = new BufferedWriter(new FileWriter("D: \\ \\ hi.txt the IO ")); //. 1, common character array mode / int * len; 
            char [] = new new char Buffer [1024]; 
            the while ((len = br.read (Buffer)) ! = -. 1) { 
                bw.write (Buffer, 0, len); 
            } 
            System.out.println ( "copy is complete ..."); * / // 2, and using readLine newLine manner 
            String Data; the while ( ! (data = br.readLine ()) = null ) { // instead of -1, because the return type String // read each line of data 
                bw.write (data); file // this is output to no wrap, so to add newLine later () method is used to wrap 
                bw.newLine (); 
            } 
            System.out.println ( " copy is complete ... "); 
        } the catch

            
           

            
            
                
 (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bw!=null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (br!=null){
                try {
                    br.close();
                } catch(IOException e) { 
                    e.printStackTrace (); 
                } 
            } 
        } 
    } 
}

Guess you like

Origin www.cnblogs.com/tanghaorong/p/12361256.html