First, on the Internet, the class diagram structure of InputStream and OutputStream
The following is the class diagram structure of reader and writer
Focus on the explanation
1.ByteArrayInputStream and Bytearrayoutputstream
As the name implies, byte input stream and byte output stream, Bytearrayinputstream () in the construction of the input stream is a byte array, read () or read(byte[] b, int off, int len) method reading a single character, if read to the end of return-1. The second is to read the byte array one at a time, Rather than read one by one.
The Bytearrayoutputstream byte array output stream creates a byte array buffer in memory, and all data sent to the output stream is saved in the byte array buffer
Without a parameter constructor, there are two methods, write (int b) and write(byte[] b, int off, int len) methods, which pass in the parameters of the byte array and output the byte array to the byte array buffer in memory through the Bytearrayoutputstream.write () method.
You can return a byte array through the Tobytearray () method, WriteTo (OutputStream out) writes the byte array of the buffer into the Out object
2.FileInputStream and FileOutputStream
The most commonly used IO stream, FileInputStream (file f), file as a data source, reads a byte through read (), or reads the length of a byte array each time through read (byte[] b).
FileOutputStream (file f), writes a byte array or byte to the file output stream through write (byte[] b), the write (int b) method. This enables file write operations.
3.PipedInputStream and PipedOutputStream
Pipeline input and output stream, the main role is to implement the import and export operations between the threads
The pipeline input stream should be connected to the pipeline output flow, and the pipeline input stream provides all the data bytes to write to the pipeline output flow. Typically, data is read from an object by a thread PipedInputStream and written by another thread to the corresponding PipedOutputStream . It is not recommended that you try to use a single thread for both objects, because this could be a deadlock thread. The pipeline inflow contains a buffer that can be separated from the read and write operations within the bounds of the buffer. If the thread that provides data bytes to the connection pipeline output stream no longer exists, the pipeline is considered corrupted.
Writes the input stream through PipedOutputStream's write () method in one thread, and then reads the contents of the output stream using the Pipedinputstream.read () method in another thread.
They are associated with the Connect (PipedInputStream out) method of PipedOutputStream via Connect (PipedInputStream in) or PipedInputStream, enabling The transfer of streams between threads.
Java IO Stream