Input and output refers to the operation of data exchange between the application and external devices and other computers, such as reading and writing hard disk data, outputting data to the monitor, and reading data from other nodes through the network. Any programming language must have input and output processing, and the Java language is no exception. The input and output data of the Java language is in the form of streams, and Java provides a large number of classes to operate on the stream, thus enabling the input and output functions.
The so-called flow refers to the same computer or network between different computers in an orderly movement of data series, Java to these different sources and targets of the data are uniformly abstracted into the data stream. Data streams can be divided into input and output streams, which represent data sequences that flow from other devices to the computer, and the output stream represents a sequence of data flowing from the computer to an external device. The data in a stream sequence can be raw data that is not processed (binary byte data), or it can be encoded to conform to a certain format, and Java provides different stream classes to handle them. Streaming input and output is characterized by data acquisition and transmission in the order of the data sequence, that is, each data must wait for the data in front of it, and so on before the data read in or sent out before being read and written. So the stream and the queue, can only be "first-out" way to read and write to the data, and can not arbitrarily choose the location to read and write.
"One" byte-based input stream
Notable places are:
①level 2 of the input stream, most will indicate the form of the data source: for example bytearray,file,piped
②level 3 of the input stream, there will be no specific data source name, but instead of a function: for example Buffered,linenumber
Therefore, the input stream of level 3 is the "encapsulation and filtering" of the level 2 input stream. In fact, the input stream of level 2 is inherited from the input stream of the FilterInputStream.
Here's an introduction to each input stream:
①bytearrayinputstream
Reads one byte of data from memory each time, and then saves it to the built-in buffer. Maintains a counter used to record the number of bytes read from the data source.
Calling the close () method of the input stream does not have any practical effect. Because it is "off" the object is---memory. instead of files. does not throw any ioexception.
②fileinputstream
The raw byte data (raw bytes) is read from the file system. Read one byte at a time
③pipedinputstream
The pipe input stream, usually one end of it, is connected to the data source, and the other end is connected to the pipe output stream (PipedOutputStream). Any data that is read from the pipe input stream is then transmitted directly to the pipeline output stream. Typically, a single thread reads data from the pipeline input stream and then to another thread, and the other thread writes the data to the pipeline output stream. If you use a single thread for read and write operations, it is easy to create a deadlock on the resource.
④sequenceinputstream
Merge multiple input streams sequentially into one input stream
⑤datainputstream
Reads bytes from the underlying other byte input stream and then translates to machine-independent raw type data (Boolean,byte,char)
⑥bufferedinputstream
Adds a "buffered" function to the underlying other byte input stream, in addition to "tag" and "reset" the input stream. When an object of this output stream is created, a built-in buffer is created.
As the underlying input stream continues to read, the data in the buffer is also refreshed differently. Read multiple bytes from the underlying input stream at once to facilitate subsequent transcoding work
"Two" byte-based output streams
It is worth noting that:
The output stream of the ① at level 2 is named after the function. Example: print,dataoutput,buffered
② the output stream at level 3, most are named in the form of a data source. Example: bytearray,file,piped
So the output stream of level 2 must depend on the level 3 output stream, in fact, the output stream of level 2 inherits the output stream called Filteroutputstream.
Here's an introduction to each output stream:
①printstream
Adding additional functionality to the underlying output stream makes it easy for the underlying output stream to output a variety of "formatted" data. Unlike other output streams, this output stream does not throw IOException, but you can check for an exception by CheckError method.
The output stream has the automatic flush function, but either writes a byte array or encounters a call to a println method, or when the character to be written out is a line break. Flush is automatically emptied. By default, all memory characters to be written out are converted to a byte stream output by the output stream in the default encoding of the platform.
②dataoutputstream
Allows the application to directly write out basic type data (Boolean, char, byte) directly to the underlying output stream (internally converted to the appropriate bytes)
③bufferedoutputstream
Provides a "buffer" function for the underlying output stream, and all write-out requests and data to be written out are buffered into the buffer of the output stream and written out at the appropriate time. Note that when the write method of this class is called, it does not necessarily immediately write out the in-memory data to the data source, but may first cache the data.
④bytearrayoutputstream
The output stream can be written to the memory byte, first cached in its own buffer. And the size of the buffer can grow automatically. If you want to extract bytes from this output stream, you can use Tobytearray, and if you want to revert to a string, you can use ToString. Shutting down the output stream does not produce any ioexception because its output is---memory and not a file.
⑤fileoutputstream
The output stream writes data to the underlying file system in the form of raw bytes (raw bytes). In some systems, sometimes only the same file is allowed
Open an output stream. So if the file is already open, opening an output stream again throws an exception.
"Three" character-based input streams
Notable places are:
①level 2 of the input stream, most will indicate the form of the data source: for example Chararray,string,file
②level 3 of the input stream, it will not appear in the specific data source name, but instead of the function: for example Buffered,linenumber
However, unlike byte-based input stream structures, FileInputStream is directly inherited from the InputStream class. But FileReader is inherited and InputStreamReader. And BufferedReader not inherit from FilterReader. Look at the following structure:
Java.io.Reader (implements Java.io.Closeable, java.lang.Readable)
Java.io.BufferedReader
Java.io.LineNumberReader
Java.io.CharArrayReader
Java.io.FilterReader
Java.io.PushbackReader
Java.io.InputStreamReader
Java.io.FileReader
Java.io.PipedReader
Java.io.StringReader
What is it then? In fact, if we know what the role of InputStreamReader is to know: its role is to act as a bridge based on the conversion between byte stream and character stream. It converts bytes read from a byte stream into characters according to the encoding.
In fact, any IO read and write to the file is ultimately done in bytes. So reading "character" is just a logical argument, then why FileReader inherit from InputStreamReader can be understood.
The following is an introduction to each reader:
①chararrayreader
Reads data directly from memory in the form of "characters". Reads one character at a time and stores it in the buffer.
②filereader
The character input stream that reads characters from a text file, the character input stream uses the system's default character set encoding and buffer size, and cannot be changed. If you need to re-adjust the encoding of the input stream, you must use InputStreamReader.
③stringreader
Read content from a string
④bufferedreader
Provides buffering capabilities for other character-based input streams for increased efficiency. Typically, any read or ReadLine request to the underlying input stream will result in direct disk access, which results in very low efficiency. When using the cached character input stream, the read request will be cached, read the batch data at the appropriate time, and then encode the conversion. This significantly improves efficiency. The buffer size of this cache input stream can be specified.
⑤linenumberreader
You can track the character input stream of "row data" that is read in. The input stream has a built-in indicator that tracks the number of rows of data that is being read in. By default, the downlink number starts at 0. Users can set/or line numbers via Setlinenumber and Getlinenumber. A row can be identified by a line break, a carriage return, and a carriage return newline character. When any one of these symbols is encountered, the value of the indicator increases by 1.
Note that the Setlinenumber method does not really change the physical location of the data in the file, but simply modifies the indicator's value.
"Four" character-based output streams
It is worth noting that:
The output stream of the ① at level 2 is named after the function. Example: print,buffered
② the output stream at level 3, most are named in the form of a data source. Example: chararray,file,string
So the output stream of level 2 must depend on the output stream of level 3, in fact the output stream of level 2 is inherited from an output stream called Filterwriter
①printwriter
Output the object data in the appropriate format to the text output stream, unlike the Printoutputstream class, which empties the buffer when it encounters a newline character. But PrintWriter does not, it empties the cache only when the print method is called. So theoretically it is more efficient than printoutputstream because it can accept more content and write to the file once, as long as the buffer allows it.
This class uses the system default row delimiter instead of "newline" N, because not all systems are wrapped by "n".
②bufferedwriter
Provides buffering for other character output streams, the buffer size of the output stream can be set, otherwise the default buffer size will be used. This class has a newline method that returns a system-based row separator, not necessarily "n". This class caches write requests and writes out to the underlying file output stream once the characters are written to a certain extent.
③chararraywriter
Similar to Bytearrayoutputstream, write characters to memory. Where a buffer is built in, the size can grow dynamically. If you want to get written characters you can use the ToCharArray method, or you can use the ToString method if you want to construct a string.
④filewriter
Writes data to a file in a character-based manner. The output stream uses the default character set encoding and buffer size of the system platform and cannot be changed. You can use OutputStreamWriter if you need to. Some systems allow only one file to be opened by an output stream at the same time, so if the file has already been opened by another output stream, the new output stream will throw an exception when it tries to open the same file.
conversion between the "five" byte stream and the character stream ①inputstreamreader:
The stream of bytes, which is used by default system encoding, can be specified as a different encoding method. is a decoding process
New BufferedReader (New InputStreamReader (system.in))
②outputstreamwriter:
The character stream--"byte stream, the default use System encoding, you can specify the encoding method. is a coding process
New PrintWriter (New BufferedWriter (New OutputStreamWriter (System.out));
Original: http://blog.163.com/zhang-_-jie/blog/static/1617843782010510111826484/
Java IO streaming architecture