IO Stream (Byte Stream/Byte Buffer Stream)

Keywords: Java Back-end

IO Flow Concepts

IO: Input/Output (input)   /  output)

Stream: is an abstract concept, which is the summary of data transmission, that is, the transmission of data between devices is called stream, which is essentially data transmission

IO streams are used to process data transfers between devices and are commonly used for file copying, file uploading, file downloading

The flow direction of IO flow is divided into two types: input flow and output flow.

There are two types of data transmitted by IO streams: character streams   Byte Stream

Prefer character streams when working with plain text files

Picture, video, audio, indeterminate file type using byte stream

Byte Output Stream:   (write data)

Create Object Method:

FileOutputStream(String name):          Create a file output stream to write to the file with the specified name

Write data by:

void write(int b)            Write the specified bytes to this file output stream one byte at a time

void write(byte[] b)             

          Writes b.length bytes from the specified byte array to this file output stream one byte array data at a time

void write(byte[] b, int off, int len)

         Writes len bytes from the specified byte array and offset off to the file output stream to write portions of the data in one byte array at a time

Line break instructions:

  • windows:\r\n

  • linux:\n

  • mac:\r

        //Create a byte output stream object that points to the aaa.txt file of path c:\aaa.txt (that is, output data to the aaa.txt folder)
        FileOutputStream fos = new FileOutputStream("c:\\aaa.txt");

        //Writes the specified byte to one byte at a time
        fos.write(2);   //Type int can be passed in directly
        fos.write("Zhang San".getBytes());     //Other data types require calling the getBytes method to convert to a byte type

        //Writes the specified array to one byte array data at a time
        byte[] bytes = new byte[1024];
        fos.write(bytes); //Write bytes array character data one at a time

        //Writes the specified array to specify a write range
        byte[] bytes1 = new byte[1024];
        fos.write(bytes1,0,100); //Writes data in one bytes1 array at a time, ranging from zero index to 100 bytes of data

        //Final shutdown
        fos.close();

Byte input stream:   (Read data)

Create Object Method:

FileInputStream(String name): Parameter passes in the path name of a file to link it and read it

Reading data method:

int read()           Read one byte at a time, the return value of type int represents the byte read, and if the return value is -1, the read is complete

int read(byte b [])           Read an array of bytes at a time. The return value represents the number of bytes read. The read content is saved in the array "b". If the return value is -1, the data is read.

//Create a byte input stream object pointing to path D:\bbb.txt to read data from this file bbb.txt
        FileInputStream fis = new FileInputStream("D:\\\\bbb.txt");

        //The int read() method reads one byte at a time, and the return value i represents the byte read, if -1 represents the end of the read
        int i;  //Loop through the data with the loop until it is finished reading
        while ((i = fis.read()) != -1) {
            System.out.println((char) i);     //The i is a byte, converted to a character for easy reading
        }

        //The int read(byte b []) method reads an array of bytes at a time, and the return value is the number of bytes read. If the return value is -1, the read is complete
        int len;    //Loop through the data with the loop until it is finished reading
        byte[] bytes2 = new byte[1024];
        while ((len = fis.read(bytes2)) != -1) {
            //Loop read data into byte array
            //Then convert the byte array to String type, with this parameter as the array to be converted, the index of the starting position of the conversion, the number of transformations
            System.out.println(new String(bytes2, 0, len));
        }

Byte Stream Copy File Implementation  

  • Case Requirements

    Copy "D:\ddd.jpg" to "aaa.jpg" on drive C (the file can be any folder)

  • Implementation Steps

    • Create byte input stream object from data source

    • Create byte output stream object from destination

    • Read and write data, copy pictures (read one byte array at a time, write one byte array at a time)

  • Release Resources

    //Create byte input stream ready to read D:\ddd.jpg file
            FileInputStream fis = new FileInputStream("D:\\\\ddd.jpg");
    
            //Create a byte output stream associated with the C:\aaa.jpg file, ready to copy data to this file
            FileOutputStream fos = new FileOutputStream("C:\\\\aaa.jpg");
    
            //Use a loop to read the files under the D drive and write them to the C drive to copy the files
            
            //First method, copy one byte at a time
            int a;
            while ((a = fis.read()) != -1) {  //Read a byte using fis.read() and assign it to a
                fos.write(a);   //Writing bytes a using fos.write() 
            }
    
            //Second method. Increase the copy efficiency of one byte array and one byte array
            int len;    //How many bytes len reads at a time
            byte[] bytes = new byte[1024];
            while ((len = fis.read(bytes)) != -1) {  //With fis.read(bytes), you can read up to one 1024 array-length byte at a time.
                fos.write(bytes, 0, len);   //Writes the read byte array to the character output stream, starting with the 0 index and writing len bytes
            }
            
            //Close after operation
            fis.close();
            fos.close();

  Byte buffer stream:

Concepts:

        Buffer streams are actually java's help in creating an array of 8192 bytes in length. We call buffer streams to read (write) an array of 8192 bytes in length to memory at once, which improves efficiency

Construct Method/Create Object:

BufferedOutputStream(OutputStream out)         

Creates a byte buffer output stream object, the parameter is a byte output stream object

BufferedInputStream(InputStream in)

Creates a byte buffer input stream object, the parameter is a byte input stream object

//Create a byte buffer output stream with a byte output stream associated with an aaa.txt file to write data to
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\aaa.txt"));

        //Write one byte at a time
        bos.write(1);
        bos.write("Zhang San".getBytes());

        //Write one byte array at a time
        byte[] bytes = {1, 2, 3, 4};
        bos.write(bytes);

        //Shutdown
        bos.close();
        
        
        
        //Create a byte buffer input stream with a byte input stream associated with an aaa.txt file to read the data in that file
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\aaa.txt"));

        //Read one byte at a time
        int a;
        while ((a = bis.read()) != -1) {
            System.out.println((char) a);
        }

        //Read an array one at a time
        int len;
        byte[] bytes1 = new byte[1024];
        while ((len = bis.read(bytes1)) != -1) {
            System.out.println(new String(bytes1, 0, len));
        }
        
        //Shutdown
        bis.close();

 

Byte Buffer Stream Copy File Implementation  

  • Case Requirements

    Copy "D:\ddd.jpg" to "aaa.jpg" on drive C (the file can be any folder)

  • Implementation Steps

    • Create byte buffer input stream object from data source

    • Create byte buffer output stream object from destination

    • Read and write data, copy pictures (read one byte array at a time, write one byte array at a time)

  • Release Resources

     //Create byte buffer input stream ready to read D:\ddd.jpg file
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\\\ddd.jpg"));
            
    
            //Create a byte buffer output stream associated with the C:\aaa.jpg file, ready to copy data to this file
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\\\aaa.jpg"));
            
    
            //Use a loop to read the files under the D drive and write them to the C drive to copy the files
    
            //First method, copy one byte at a time
            int a;
            while ((a = bis.read()) != -1) {  
                bos.write(a);   
            }
    
            //Second method. Increase the copy efficiency of one byte array and one byte array
            int len;    //How many bytes len reads at a time
            byte[] bytes = new byte[1024];
            while ((len = bis.read(bytes)) != -1) {  
                bos.write(bytes, 0, len);   
            }
    
            //Close after operation
            bis.close();
            bos.close();

Posted by jhenary on Fri, 26 Nov 2021 15:42:29 -0800