BufferedOutputStream buffered byte output stream

7.BufferedOutputStream

1. Output byte stream

---- the base class and abstract class of OutputStream output byte stream

---------- output byte stream of output data from FileOutputStream to file

------ | BufferedOutputStream buffer output byte stream. The purpose of BufferedOutputStream is to improve the efficiency of data output to files. It internally maintains an array of 8kb(8192 bytes).

 

2. To use BufferedOutputStream:

1. Find the target file

2. Build data channel

3. Write out the data to the byte array maintained by BufferedOutputStream

4. Write out the data in byte array (memory) to hard disk file.

 

3. Details of bufferedoutputstream

1. When using BufferedOutputStream to write data, its write method writes the data to the array it maintains internally, rather than directly to memory. Because BufferedOutputStream does not have the ability to write data to a file.

2. Three cases of using BufferedOutputStream to write data to files

First, use the write method of BufferedOutputStream to write data to the byte array (that is, memory). There are three situations when writing hard disk files from memory:

(1) use the flush method to brush the data in memory into the hard disk file.

(2) use the close method to brush the data in memory into the hard disk file. In fact, it internally calls two methods, flush and close, to brush the input flush into the hard disk file, and then close the FileOutputStream resource.

(3) when the byte array is full of data (that is, the byte array has stored 8kb bytes), it will automatically brush the data in the array into the hard disk file.

4. cases

public class Dome2 {
    public static void main(String[] args) {
        // 1. Find the target file
        File file = new File("E:\\aa\\bb\\BufferOutputStream.txt");

        // 2. Build a passage
        FileOutputStream fileOutputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            // BufferedOutputStream
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

            // 3. Write out the data to the byte array maintained by BufferedOutputStream
            String str = "hello word!!!";//Data to write
            byte[] bytes = str.getBytes();//Convert string to byte array
            bufferedOutputStream.write(bytes);//Write byte input to the byte array maintained inside BufferedOutputStream
        } catch (FileNotFoundException e) {
            System.out.println("File search exception......");
            throw new RuntimeException(e);
        } catch (IOException e) {
            System.out.println("Exception writing out data to memory......");
            throw new RuntimeException(e);
        } finally {
            try {
                // 4. Write out the data in byte array to hard disk file
                //fileOutputStream.flush(); / / brush the data in memory into the hard disk file
                bufferedOutputStream.close();//Within the close method of BufferedOutputStream, it is also the flush method called
                /*
                    BufferedOutputStream Source code of the close method of:
                        public void close() throws IOException {
                            try (OutputStream ostream = out) {
                                flush();
                            }
                        }
                */
                // When the 8 KB byte array maintained by BufferedOutputStream is full, the flush method will also be called automatically to write out the data to the hard disk file.
            } catch (IOException e) {
                System.out.println("Exception in writing out data in memory to hard disk......");
                throw new RuntimeException(e);
            }
        }
    }
}

Conclusion:

1. The use method of BufferedOutputStream is similar to that of BufferedInputStream, except that the write method of BufferedOutputStream is to write data to an internally maintained byte array rather than directly to a hard disk file.

2. Write the data from the memory array to the hard disk file. The plug method called is the same as the close method, because the close method also calls the flush method internally.

3. For notes of bufferedinputstream, see the previous article: https://blog.csdn.net/qq_38082911/article/details/103951009

 

 

 

 

 

Published 19 original articles, won praise 11, visited 10000+
Private letter follow

Posted on Sun, 12 Jan 2020 20:36:45 -0800 by Emperor_Jackal