Background
Java's core library, Java.io, provides a comprehensive IO interface. Include: file reading and writing, standard equipment output, etc. Io in Java is input and output on a stream basis, and all data is serialized to the output stream or read in from the input stream. --Baidu Encyclopedia
There are many classes in java.io, how do we know exactly when we should use them?
Goal
The goal of this paper is to comb the relationships of each class in java.io and to understand the application scenarios of each class.
Concept
data Sources , data streams , destinations . Every time you look at the relevant code for Java IO, ask yourself who each of these three people is referring to. And this article is mainly about data flow.
Classified
The various "streams" are categorized into different classes according to different classification methods.
Although a variety of "streams" can be separated from different angles, they are not independent and intersect, as FileInputStream is the input stream and is also a byte stream or a node flow.
Distinguish between the input stream and the output stream, whether the name of the class has in, or out, or R or W. Distinguish between byte stream and character stream, basically see the end is stream or reader, Writer. How to differentiate between node flow and processing flow, it is necessary to talk about the decorator pattern in design mode.
I just said three kinds of classifications have crossover, the stream of both nodes and the processing flow in the byte stream. The character stream is the same. Here we take the example of byte-stream, using the decorator pattern to illustrate the relationship between the node flow and the processing flow.
To learn this part, we liken the data stream to a pipe, if the node flow is a pipe directly connected to the data source or destination, then the processing flow is the tube that is set on the pipe, as shown in.
Decorator mode with Java IO
Decorator mode is to dynamically attach responsibility to the object, extending the behavior of the object. We can use the process flow to decorate a layer of the node flow as needed. This is flexible and avoids the "class explosion" in a combination way. However, a variety of "stream" classes have also caused us to use the API difficult. But understand the decorator mode, you can according to the needs of arbitrary packaging, it is not messy.
Common use of the class
Node Stream |
Use |
FileInputStream |
Read data from a file |
Bytearrayinputstream |
Read data from an in-memory byte array |
Process Flow |
Use |
new behavior/remarks |
Bufferedinputstream |
Processing stream with buffers to avoid accessing the hard disk every time a byte is read, increasing the access rate and reducing damage to the hard disk |
such as: ReadLine () |
DataInputStream |
Convert bytes directly into a variety of basic types (int,double ... ) to read |
such as: Readdouble () |
ObjectInputStream |
Convert bytes directly into objects for reading |
Serializable,transient,externalizable |
InputStreamReader |
Convert bytes to characters to read, for Chinese characters if read by Byte, I don't know what word it is after reading it. |
—— |
Summarize
The article does not enumerate all the input and output streams in Java IO, it is important to extrapolate and digest according to the above summary. Specific details, also check the API documentation.
Java i/o-Comb various "streams"