Class structure diagram for I/O
Classification of I/O
Depending on the type of data processed: byte stream and character stream.
It is divided into: input stream and output stream according to data flow.
Flow can be divided into node flow and processing flow.
Node stream
Connect directly to the data source
Process Flow
Used in conjunction with the node stream, and then nested one layer on the basis of the node flow. Improve the read efficiency of your files.
Input stream
InputStream
The function is to read the data generated by the data source in the form of bytes.
Reader
The function is to read the data generated by the data source in the form of a character.
Output stream
OutputStream
The function is to write the data in bytes on the receiving end.
Writer
The function is to write the data in the form of a character on the receiving end.
Common streams
Buffered streams
Function: Buffer stream provides buffer, first write the data to buffer, when the buffer is full, then write the contents of the buffer to the file. This will improve read efficiency.
Object Flow
Function: Used to write/read information about an object, making the object persistent.
Precautions for use:
1. If you want to write an object to disk, then the class that the object belongs to must be serialized, implement the Serializable interface, serializable interface is not any method, is a tag interface
2. If the object belongs to the class member variable changes, you read the original object is an error, if you want to resolve the error, ensure that Serialversionuid is unique.
3. If you do not want to save some information to disk, you can use the Transient keyword to modify the member variable
4. If another class is referenced in a class, the other class also implements the Serializable interface.
Pipe flow
Role: Communication between two threads can be performed. One thread as the pipe output stream and the other as the pipeline input stream.
Convert stream
Function: Used for conversion of Byte stream and character stream.
InputStreamReader: Converting a byte stream to a character stream
OutputStreamWriter: Converting a character stream to a byte stream
Summary of Java I/O streams