Java_ Self writing byte buffer stream, input stream and output stream for data transmission

Java_ Self writing byte buffer stream for data transmission

I. byte buffered output stream

​        java.io.BufferedOutputStream extends OutputStream

BufferedOutputStream: byte buffered output stream.

Common member methods inherited from parent class:

public void close(): close this output stream and release any system resources associated with this stream.

public void flush(): flushes this output stream and forces any buffered output bytes to be written out.

public void write(byte[] b): writes b.length bytes from the specified byte array to this output stream.

public void write(byte[] b, int off, int len): write len bytes from the specified byte array and output to this output stream starting from offset off.

public abstract void write(int b): outputs the specified bytes to the stream.

Construction method:

BufferedOutputStream(OutputStream out) creates a new buffered output stream to write data to the specified underlying output stream.

BufferedOutputStream(OutputStream out, int size) creates a new buffered output stream to write data with the specified buffer size to the specified underlying output stream.

Parameters:

OutputStream out: byte output stream, which can be passed to FileOutputStream. The buffer stream will add a buffer to FileOutputStream to improve the writing efficiency of FileOutputStream

int size: Specifies the size of the internal buffer of the buffer stream, not the default.

Use steps [important]

① create a FileOutputStream object and bind the destination to be output in the construction method

② create BufferedOutputStream object and pass FileOutputStream object in the construction method to improve the efficiency of FileOutputStream object

③ use the write method in BufferedOutputStream object to write the data into the internal buffer

④ use the method flush in the BufferedOutputStream object to flush the data in the internal buffer into the file

⑤ release resources (call flush method to refresh data first)

/**
 * @program: code
 * @description: Byte buffered output stream
 * @author: yangzhihong
 * @create: 2022-02-24 11:30
 **/
public static void main(String[] args) throwsIOException {

    //1. Create a FileOutputStream object and bind the destination to be output in the construction method

    FileOutputStream fos = new FileOutputStream("E:\\a.txt");

    //2. Create BufferedOutputStream object and pass FileOutputStream object in the construction method to improve the efficiency of FileOutputStream object

    BufferedOutputStream bos = newBufferedOutputStream(fos);

    //3. Use the write method in the BufferedOutputStream object to write the data to the internal buffer

    bos.write("I write the data to the internal buffer".getBytes());

    //4. Use the method flush in the BufferedOutputStream object to flush the data in the internal buffer into the file

    bos.flush();

    //5. Release resources (the flush method will be called first to refresh the data, and Part 4 can be omitted)

    bos.close();

}

2, Byte buffered input stream

​        java.io.BufferedInputStream extends InputStream

BufferedInputStream: byte buffered input stream

Member methods inherited from parent class:

int read() reads the next byte of data from the input stream.

int read(byte[] b) reads a certain number of bytes from the input stream and stores them in buffer array B.

void close() closes this input stream and releases all system resources associated with the stream.

Construction method:

BufferedInputStream(InputStream in) creates a BufferedInputStream and saves its parameters, the input stream in, for future use.

BufferedInputStream(InputStream in, int size) creates a BufferedInputStream with a specified buffer size and saves its parameter, input stream in, for future use.

Parameters:

InputStream in: byte input stream, which can be passed to FileInputStream. The buffer stream will add a buffer to FileInputStream to improve the reading efficiency of FileInputStream

int size: Specifies the size of the internal buffer of the buffer stream, not the default.

Use steps [important]:

① create a FileInputStream object and bind the data source to be read in the construction method

② create BufferedInputStream object and pass FileInputStream object in the construction method to improve the reading efficiency of FileInputStream object

③ use the read method in BufferedInputStream object to read the file

④ release resources

/**
 * @program: code
 * @description: Byte buffered input stream
 * @author: yangzhihong
 * @create: 2022-02-24 11:40
 **/

public static void main(String[] args) throwsIOException {

        //1. Create a FileInputStream object and bind the data source to be read in the construction method
        FileInputStream fis = new FileInputStream("10_IO\\a.txt");

        //2. Create BufferedInputStream object and pass FileInputStream object in the construction method to improve the reading efficiency of FileInputStream object
        BufferedInputStream bis = newBufferedInputStream(fis);

        //3. Use the read method in BufferedInputStream object to read the file
        //int read() reads the next byte of data from the input stream.
        /*int len = 0;//Record the bytes read each time*/
        while((len = bis.read())!=-1){
            System.out.println(len);
        }
        //int read(byte[] b) reads a certain number of bytes from the input stream and stores them in buffer array B.
        byte[] bytes =new byte[1024];//Store data read each time
        int len = 0; //Record the number of valid bytes read each time
        while((len = bis.read(bytes))!=-1){
            System.out.println(new String(bytes,0,len));
        }
        //4. Release resources
        bis.close();
    }

3, Self writing byte buffer stream

1. Through the self writing buffer stream, we can better understand the underlying principle of data transmission in the stream

2. Not only need to be able to use, but also need to fully understand and simplify each line of code

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

/**
 * @program: code
 * @description: Self writing decoration design mode (enhance the performance of byte output node stream writing data)
 * @author: yangzhihong
 * @create: 2022-02-24 14:45
 **/

class MyBufferedOutputStream{
    //Member properties
    private OutputStream out;
    //Define buffer size
    private static final int DEFAULT_BUFFER_SIZE = 8192;
    //Define byte array
    private byte[] buf_array;
    //Define subscript index
    private int index = 0;

    //Construction method, create a byte array object, and assign the length of the transmitted data to the object
    private MyBufferedOutputStream(int bufSize){
        this.buf_array = new byte[bufSize];
    }
    //Construction method to process the transmitted byte output stream
    public MyBufferedOutputStream(OutputStream out){
        this(DEFAULT_BUFFER_SIZE);
        this.out = out;
    }
    //Construction method to process the passed path string
    public MyBufferedOutputStream(String filePath) throws FileNotFoundException {
        this(DEFAULT_BUFFER_SIZE);
        this.out = new FileOutputStream(filePath);
    }
    //Create a write() write method and move the subscript of the byte array
    public void write(int date){
        this.buf_array[index++] = (byte) date;
    }
    //Create a flush() refresh method to refresh the buffer and write the data in the buffer into the file
    public void flush() throws IOException {
        this.out.write(this.buf_array,0,index);
    }
    // When you create a close() method to close the flow, you actually call the flush() method
    // It is also at this step that the byte output stream writes data to the target file
    public void close(){
        try {
            flush();    // refresh buffer 
            this.out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public class MyBuffereddOutputStreamDemo {
    // Program entry, test self writing byte buffer stream
    public static void main(String[] args) throws IOException {
        // Create an output stream object. If there is no target file, the target file will be created automatically
        OutputStream outputStream = new FileOutputStream("C:\\Users\\yang\\Desktop\\yangsir.txt");
        // Wrap the output stream with a self writing byte buffer stream (tube)
        MyBufferedOutputStream myBufferedOutputStream = new MyBufferedOutputStream(outputStream);
        // test
        myBufferedOutputStream.write(97);
        myBufferedOutputStream.write(98);
        // Close the stream to prevent excessive memory consumption
        myBufferedOutputStream.close();
    }
}

Keywords: Java Eclipse github Back-end

Added by black.horizons on Thu, 24 Feb 2022 10:34:08 +0200