Stream is a very visual concept. When the program needs to read data, it will open a stream to the data source, which can be a file, memory, or network connection. Similarly, when the program needs to write data, it will open a stream to the destination. At this time, you can imagine that the data is like a "stream" in it, such:
Java. Io package is a set of stream classes divided:
Byte stream: the abstract parent class is inputstreamoutputstream.
Extract stream: Abstract The parent class is reader and writer.
[Note]: (input/output) in I/O) stream is nothing more than character-based stream, byte-based stream, and byte-based Stream Conversion to character-based stream.
1. inputstream
Architecture of the inputstream class
1.1. pipedinputstream: implements the pipe concept and is mainly used in the process. An input stream in a pipeline refers to two receivers of a communication pipeline. One thread sends data through the output stream of the pipeline, while the other thread reads data through the input stream of the pipeline, which enables communication between the two threads.
Its constructors include:
-Pipedinputstream () is used to create an input stream of the pipeline. It is not connected to an output stream of the pipeline.
-Pipedinputstream (pipedoutputstream) creates a pipeline input stream, which is connected to a pipeline output stream.
1.2. fileinputstream class: reads data from an actual disk file and uses a file as inputstream to read the file.
Its constructors include:
-Fileinputstream (File) creates an input file stream to read data from the specified file object.
-Fileinputstream (filedescriptor) creates an input file stream and reads data from the specified file descriptor.
-Fileinputstream (string) creates an input file stream to read data from a file with the specified name.
It uses the following methods:
-Read () reads one byte of data from the current input stream.
-Read (byte []) reads B. length bytes of data in the current input stream to a byte array.
-Read (byte [], Int, INT) reads len bytes of data in the input stream into a byte array.
1.3. bytearrayinputstream class: read/write operations are performed in byte arrays, that is, a buffer in the memory is used as inputstream.
Its constructors include:
-Bytearrayinputstream (byte []) creates a new byte array input stream, which reads data from the specified byte array.
-Bytearrayinputstream (byte [], Int, INT) creates a new byte array input stream, which reads data from the specified byte array.
MARK: the byte array is not copied.
1.4. sequenceinputstream: combines multiple inputstreams into one inputstream. The "sequential input stream" class allows applications to consecutively combine several input streams and make them appear like a single input stream. Each input stream is read until it reaches the end of the stream. Then, the "sequence input stream" class closes the stream and automatically switches to the next input stream.
Constructor:
-Sequenceinputstream (enumeration) creates a new sequence input stream and initializes it with the enumerated value of the specified input stream.
-Sequenceinputstream (inputstream, inputstream) creates a new sequence input stream, initialized to first read the input stream S1, and then read the input stream S2.
1.5. stringbufferinputstream class: similar to bytearrayinputstream, a string is used as an internal buffer, or a String object is used as an inputstream.
[Comment] The stringbufferinputstream method is not recommended. This class cannot correctly convert characters into bytes.
Similar to JDK 1.1, the best way to create a stream from a string is to use the stringreader class.
Its constructors include:
-Stringbufferinputstream (string) creates an input stream string for reading data from a specified string.
2. outputstream
Defines the method used to write a byte or byte array.
Architecture of the outputstream class
2.1. fileoutputstream class: write data into files in byte streams. (If the file does not exist, it is written after the file is created ). The file here is directed to file or filedescriptor.
Its constructors include:
-Fileoutputstream (File) creates a file output stream and outputs data to the specified file object.
-Fileoutputstream (filedescriptor) creates a file output stream and outputs data to the specified file descriptor.
-Fileoutputstream (string) creates a file output stream and outputs data to the file with the specified name.
-Fileoutputstream (string, Boolean) creates an output file by specifying the system file name.
2.2. pipedoutputstream: the pipe output stream refers to the sending end of a communication pipeline. One thread sends data through the output stream of the pipeline, while the other thread reads data through the input stream of the pipeline, which enables communication between the two threads.
Its constructors include:
-Pipedoutputstream () creates an output stream of the pipeline, which is not connected to an input stream of the pipeline.
-Pipedoutputstream (pipedinputstream) creates an output stream of the pipeline, which is connected to an input stream of the pipeline.
2.3. bytearrayoutputstream class: Creates a buffer in the memory and stores information in a buffer in the memory. This class implements an output stream that writes data in the form of byte arrays. It automatically expands when data is written to the buffer zone. The tobytearray () and tostring () methods are used for data retrieval.
Its constructors include:
-Bytearrayoutputstream () creates a new byte array output stream.
-Bytearrayoutputstream (INT) creates a new byte array output stream with the buffer capacity of the specified size bytes.
Common methods include:
-Tostring (string) converts the buffer content to a string based on the specified character encoding and converts bytes to characters.
-Write (byte [], Int, INT) writes the len bytes starting from offset off in the specified byte array to the output stream of the byte array.
-Write (INT) writes the specified byte to the output stream of the byte array.
-Writeto (outputstream) uses out. Write (BUF, 0, count) to call the write method of the output stream to write all the content of the byte array output stream to the specified output stream parameter.
2.4. filteroutputstream class: High-level output stream
File: provides the ability to locate local file systems, description files, and directories. It is the only object in the Java. Io package that references the actual disk files.
Stream classes can be divided:
Bottom layer: the method of reading and writing data in bytes.
High-level filter stream: used to read and write high-level information;
High-rise streams require the bottom layer as the basis.
The sub-classes of the filterinputstream class include:
* Datainputstream class: provides the ability to read arbitrary objects.
* Dataoutputstream class: provides the ability to write arbitrary objects.
* Bufferedinputstream class: allows the program to read data from the stream one byte at a time.
* Bufferedoutputstream class: allows the program to write data to the stream one byte at a time.
* Printstream class: used to write text or basic types
3. Reader
Read character type.
Reader class Architecture
Bufferedreader: a subclass of the reader class. It accepts the reader object as a parameter, adds a character buffer to the reader object, allocates memory storage space for data input, and makes data access more effective.
Chararrayreader: allows the use of character arrays as input
Its constructors include:
-Chararrayreader: corresponds to bytearrayinputstream
-Chararrayreader (char []) creates a chararrayreader with a specified character array.
-Chararrayreader (char [], Int, INT) creates a chararrayreader with a specified character array.
Inputstreamreader: reads bytes from the input stream and converts them into characters.
Filereader: make it possible to read character files, corresponding to fileinputstream.
Filterreader: allows reading and filtering shards.
Pipedreader: corresponds to pipedinputstream.
Stringreader: String read character, which corresponds to stringbufferinputstream.
4. Writer
Write character type.
Writer class Architecture
Bufferedwriter: Buffer data to the character output stream
Chararraywriter: allows the character buffer to be used as the output stream, which corresponds to bytearrayoutputstream.
Filewriter: allows you to write data of the character type to a file, which corresponds to fileoutputstream.
Printwriter: contains some methods that make it easy to generate formatted output.
Stringwrite: There is no byte-oriented stream corresponding to it.
Filterwriter: used to write and filter shard streams.
Pipedwrite: corresponds to pipedoutputstream
Conversion between two different directed streams
Inputstreamreader and outputstreamreader: convert a byte-based stream into a character-based stream. The inputstreamreader class is a bridge between byte and the bytes stream: It reads bytes and converts them to the bytes stream according to the specified encoding method. The encoding method may be specified by the name or the default encoding method acceptable to the platform.
Inputstreamreader (inputstream) uses the default character encoding method to create an inputstreamreader.
Inputstreamreader (inputstream, string) uses a named character encoding method to create an inputstreamreader.
The read () method of inputstreamreader has multiple overload methods to read one or more bytes from the basic byte input stream.
To achieve higher efficiency, we consider using bufferedreader to encapsulate inputstreamreader,
Bufferedreader in = new bufferedreader (New inputstreamreader (system. In ));
Outputstreamwriter writes multiple characters to an output stream and converts multiple characters to bytes based on the specified character encoding.
Each outputstreamwriter combines its own chartobyteconverter, which serves as a bridge from the bytes stream to the byte stream.
General Usage principles of Java I/O:
1. Classification by data source (destination:
1. File: fileinputstream, fileoutputstream, filereader, filewriter
2. byte []: bytearrayinputstream, bytearrayoutputstream
3. Char []: chararrayreader, chararraywriter
4. String: stringbufferinputstream, stringreader, stringwriter
5. network data streams: inputstream, outputstream, reader, and writer
Ii. formatted output score:
1. output to be formatted: printstream, printwriter
Iii. cache score:
1. Buffer: bufferedinputstream, bufferedoutputstream, bufferedreader, bufferedwriter
Iv. Data Format:
1. binary format (as long as it cannot be determined that it is plain text): inputstream, outputstream and all its subclasses
2. plain text format (including English and Chinese characters or other encoding methods): reader, writer, and all subclasses with reader and writer
5. Input/Output score:
1. Input: reader, a subclass of the inputstream type
2. Output: a subclass of the writer and outputstream types.
6. Special needs:
1. Conversion class from stream to reader: inputstreamreader and outputstreamwriter
2. Object input and output: objectinputstream and objectoutputstream
3. inter-process communication: pipeinputstream, pipeoutputstream, pipereader, and pipewriter
4. Merge input: sequenceinputstream
5. Special requirements: pushbackinputstream, pushbackreader, linenumberinputstream, and linenumberreader
The general guidelines for deciding which class to use and its construction process are as follows (special requirements are not considered ):
First, consider what is the most primitive data format: Principle 4
Second, whether input or output: Principle 5
Third, whether stream conversion is required: Principle 6
Fourth, what is the data source (destination): Principle 1
Fifth, whether to buffer: Principle 3 (Note: it must be noted that Readline () is defined, and there are more special input or output methods than read and write)
Sixth, whether to format the output: Principle 2