J2SE quick advanced -- IO stream, j2se advanced io
Concept of IO stream
An IO stream is an abstraction that flows data from one place to another, just as a flow of water flows from one end of the pipeline to the other. In a program, all data is transmitted and saved as a stream.
All the stream classes provided by JDK are in the java. io package, and these classes are inherited from the following four abstract stream classes: InputStream, OutputStream, Reader, and Writer.
Note:
InputStream: streams inherited from InputStream are used to input data into the program, and the data units are all bytes (8 bits ).
OutputStream: the stream inherited from OutputStream is used by the program to output data, and the data unit is bytes (8 bits ).
Reader: the stream inherited from Reader is used to input data into the program, and the data unit is all characters (16 bits ).
Writer: the stream inherited from Writer is used by the program to output data, and the data unit is a character (16 bits ).
IO stream classification
★IO streams can be classified by flow direction.Input streamAndOutput stream. Generally, data input and output are taken into account from the perspective of the program. Therefore, the input stream means that the program reads data from the file, that is, the data flows from the file to the program; the output stream is to make the data flow from the program to the file.
★IO streams are classified by transmission unit.Byte streamAndGhost stream. As the name suggests, byte streams transmit data based on bytes, while byte streams transmit data based on Unicode characters occupying 2 bytes. Here, let's recall the basic knowledge: 1 Byte (Byte) = 8 bit (BIT), because the characters in Java use Unicode encoding, so each character occupies two bytes, the storage space occupied by basic data types in Java is:
Type |
Occupied storage space |
Char |
2 bytes |
Byte |
1 byte |
Short |
2 bytes |
Int |
4 bytes |
Long |
8 bytes |
Float |
4 bytes |
Double |
8 bytes |
Byte streams can be used for any type of objects, including binary objects. The byte stream can only process characters or strings. 2. A byte stream provides the ability to process any type of IO operations, but it cannot directly process Unicode characters, while a bytes stream can.
When you understandFlow DirectionAndTransmission UnitWhen using these two classification methods, you can look at the thought map above:
Both Reader and InputStream belong to the input stream, and their sub-classes are responsible for reading data from the data source. Both Writer and OutputStream belong to the output stream, and their sub-classes are responsible for writing data to the specified position.
Both Reader and Writer belong to the bytes stream, and their child classes transmit data based on characters. Both InputStream and OutputStream belong to the byte stream, and their child classes transmit data based on bytes.
★It can also be classified based on the functions of the IO stream, dividedNode streamAndStream Processing. A node stream reads and writes data from or to a specific node. processing a stream is a connection and encapsulation of an existing stream. It can be viewed as an optimization or filtering of an existing stream.
Let's take a look at the huge IO family in java:
Here we first have a global understanding of the IO mechanism in java, and will further learn and summarize these four abstract classes and their subclasses.