First, the classification of the flow
1. In accordance with the direction divided into:
(1) Input stream: reads data from the data source into the program. You can only read data from it, and you cannot write data to it. The input streams in the IO package inherit from the abstract class InputStream or reader.
(2) Output stream: Writes data from the program to the destination. Data can only be written from, and not read from, the output stream in the IO package inherits from the abstract class OutputStream and writer.
Each input stream in the I/O library can be found for the corresponding output stream.
2. According to the smallest unit of processing data is divided into:
(1) Byte stream: A byte stream is a bytes for the smallest unit of data transfer, the byte stream in the IO packet inherits from the abstract class InputStream or OutputStream.
(2) Character stream: Character stream is the smallest unit of char for data transfer. The stream of characters in the IO packet is inherited from the abstract class reader or writer.
The I/O Library makes this distinction in order to satisfy the need to process textual data. Byte is 8 bits, and char is 16 bits. In Java, various language literals are encoded in Unicode, Unicode encoding is based on 16-bit char, so it is more efficient and convenient to read and write text data with character streams. Some binary data reads and writes, the image, the sound and so on, still must pass through the byte stream to complete.
The class name of the byte stream ends with InputStream or OutputStream, and the class name of the character stream ends with reader or writer. Byte stream corresponds to the memory variable type is byte[], the character stream corresponding memory variable type is char[].
3. According to the function of the flow is divided into:
(1) Node flow: A node stream is a stream that can read and write data directly from or to a particular data source. Also become a low-level stream.
Classes derived from InputStream or reader have a basic read () method that reads a single byte or byte array, and has a basic write () method for all classes derived from OutputStream or writer. Used to write a single byte or an entire byte array.
(2) Processing flow: The processing stream is not directly connected to the device, but is connected to the existing stream (node flow or processing flow), through the processing of the data for the program to provide more powerful read and write capabilities, also become an advanced stream.
Differentiate between advanced and low-level streams: View the constructor of the class, if the class's constructor has an existing stream as an argument, then it is an advanced stream, otherwise it is a low-level stream.
I/O operation steps:
(1) Create a stream: Select the stream based on the data source and the specific action, and then create the flow. Create a data channel between the memory and the data source to transfer the data.
(2) Operation Flow: reads the data into memory or writes the in-memory data to the data source.
(3) Close stream: After the flow operation is finished, all system resources associated with the stream are released.
Second, the document class
The file class in the Java.io package provides basic functionality for managing disk files and directories. We can consider a file object to be a string representing the name and location of a document or directory. The file class has four methods of construction:
Public file (String pathname): Creates a file object that is associated with the specified path name.
Public file (String parent,string child): Creates a File object with the specified parameters. The parameter parent represents a directory, and the parameter child represents a subdirectory or file in the parent.
Public file parent,string: the same as the previous one, except that the directory is represented by a File object instead of a string.
Public file (Uri Uri): Creates a file object with the given Java.net.URI object. The URI is the Uniform Resource identifier, and the URI of a file is the "File:///directory/File name" format.
Because the file object resembles a string and represents only the path name of a file or directory, these constructors can execute successfully even if the specified file or directory does not exist.
Third, the byte stream
A byte stream is used to process a binary file. All byte streams inherit from the abstract class InputStream and OutputStream two parent classes. Where InputStream is the parent class that reads the byte stream, OutputStream is the parent class that writes the byte stream
1. Low-level byte stream
There are three categories based on input and output devices or data sources:
(1) FileInputStream and FileOutputStream classes for reading and writing binary files, whose data source is a disk file.
(2) The Bytearrayinputstream and Bytearrayoutputstream classes that read and write operations to byte arrays of memory buffers, whose data source is an in-memory byte array.
(3) The PipedInputStream and PipedOutputStream classes that read and write to the thread pipeline, whose data source is the thread pipeline.
2. Advanced byte stream
The advanced byte stream encapsulates the low-level byte stream and has many extensions of functionality.
(1) Byte buffer stream
Bufferedinputstream and Bufferedoutputstream can increase the buffer function for objects of the InputStream and OutputStream classes. The Bufferedinputstream data member B is a byte array that defaults to 2048 bytes. When the data source is read, Bufferedinputstream will try to fill the B. When you use the Read () method, you are actually reading data from data member B instead of directly reading the data source. When the data in B is insufficient, the read () method of the given InputStream object is then implemented to extract the data from the specified device.
The data member of the Bufferedoutstream is a byte array, which defaults to 512 bytes. When using the Write () method, the data is written to data member B, and when B is full, the write () method of the given OutputStream object is implemented, and the B data is written to the destination instead of being written to the destination each time.
Iv. character Stream
Character streams are used to process text files. All character streams inherit from the abstract class reader and writer two parent classes. Where reader is the parent class that reads a stream of characters, writer is the parent class that writes a stream of characters.
Reader, like InputStream, is used to read data from a stream, and reader reads the data in characters.
Writer is similar to outputstream for writing data from a stream, and writer writes the data in characters.
JAVA I/O system