Black Horse programmer _ JAVA. IO stream learning notes

Source: Internet
Author: User

1. Description of java. io

Provides system input and output through data streams, serialization, and file systems.

Ii. Stream

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 as dynamic as the "stream" in it.

Iii. Principles of Java Stream Input and Output

Java abstracts data from different sources and targets into data streams. The input and output functions of the Java language are very powerful and flexible. The disadvantage is that it seems that the input and output code is not very concise, Because you often need to wrap many different objects.

In Java class libraries, the I/O part of the content is very large, because it involves a wide range of fields: standard input and output, file operations, network data streams, string streams, object stream and zip file stream.

1. Java stream classification

By flow: input stream: the stream from which the program can read data; output stream: the stream in which the program can write data.

Data transmission unit: byte stream: data stream transmitted in bytes; character stream: data stream transmitted in characters

By function: node stream: Used to directly operate the stream of the target device; filter stream: Used to link and encapsulate an existing stream, data processing provides powerful and flexible read/write functions for programs.

Traffic and word throttling

The origin of the character stream: because of the different data encoding, there is a stream object for efficient character operations. In essence, when reading data based on byte streams, the specified code table is checked. Differences between byte stream and byte stream:

Different read/write units: byte streams are measured in 8 bits and byte streams are measured in characters. Based on the ing characters in the code table, multiple bytes may be read at a time.

Different Processing objects: byte streams can process all types of data (such as slice and avi), while character streams can only process data of character types.

Conclusion: as long as the plain text data is processed, the use of the plain stream is preferred. In addition, byte streams are used.

Input stream and output stream

Only read operations can be performed on the input stream, and only write operations can be performed on the output stream. Different streams must be used in the program based on different features of the data to be transmitted.

Java IO Stream Object

(1). inheritance relationship of input byte streams in InputStreamIO

InputStream is the parent class of all input byte streams. It is an abstract class.

ByteArrayInputStream, StringBufferInputStream, and FileInputStream are three basic media streams that read data from Byte arrays, StringBuffer, and local files respectively. PipedInputStream reads data from pipelines shared with other threads. Piped-related knowledge will be introduced separately later.

ObjectInputStream and all the sub-classes of FilterInputStream are decorative streams (the main character of the decoration mode ).

(2) Output byte stream OutputStream

OutputStream is the parent class of all output byte streams. It is an abstract class.

ByteArrayOutputStream and FileOutputStream are two basic media streams, which write data to Byte arrays and local files respectively. PipedOutputStream writes data to the pipe shared with other threads,

ObjectOutputStream and all the subclasses of FilterOutputStream are decorative streams.

(3). Correspondence between input and output of byte streams

 

In the figure, the blue part is the main corresponding part, and the red part is the non-corresponding part. The purple dotted line indicates that these streams are generally used together. From the figure above, we can see that the byte stream in Java IO is extremely symmetric. "Exist and make sense." Let's look at several classes that are not symmetric in these byte streams!

LineNumberInputStream obtains the corresponding row number when it reads data from the stream. When and where the branch is determined by the change class, there is not such a line number in the original. There is no corresponding part in the output part. We can create a LineNumberOutputStream by ourselves. There will be a baseline line number when we first write data, and a line number will be added to the next line each time we encounter a line change, it looks okay, too. It seems that it is not streaming.

PushbackInputStream is used to view the last byte and put it into the buffer zone if it is not satisfied. It is mainly used in Compiler syntax and lexical analysis. The BufferedOutputStream of the output is almost similar.

The StringBufferInputStream has been Deprecated and should not appear in the InputStream part, mainly because the String should belong to the shard stream range. It has been deprecated. Of course, it is not necessary for the output part! It is also allowed to exist only to keep the version backward compatible.

SequenceInputStream can be considered as a tool class. It reads two or more input streams as one input stream in sequence. It can be removed from the IO package completely without affecting the structure of the IO package, but making it "pure"-pure Decorator mode.

PrintStream can also be considered as an auxiliary tool. Data can be written to other output streams, or FileInputStream. The internal implementation is still buffered. Essentially, it is a tool for the comprehensive use of other streams. I/O packages can be kicked out! System. out and System. out are PrintStream instances!

(4). character input stream Reader

Reader is the parent class of all input streams. It is an abstract class.

CharReader and StringReader are two basic media streams. They read data from Char arrays and strings respectively. PipedReader reads data from pipelines shared with other threads.

BufferedReader is obviously a decoration device, and its sub-classes are used to decorate other Reader objects.

FilterReader is the parent class of all custom decorative streams. Its subclass PushbackReader is used to decorate the Reader object and adds a row number.

InputStreamReader is a bridge between byte streams and primary streams. It converts byte streams into primary streams. FileReader can be said to be a commonly used tool class to achieve this function. In its source code, the FileInputStream is obviously used as a Reader method. We can get some tips from this class. The usage and usage of each class in Reader are basically the same as that in InputStream. There will be a ing between Reader and InputStream.

(5). character output stream Writer

Writer is the parent class of all output streams. It is an abstract class.

CharArrayWriter and StringWriter are two basic media streams that write data to Char arrays and strings respectively. PipedWriter writes data to pipelines shared with other threads,

BufferedWriter is a decorator that provides the buffer function for Writer.

PrintWriter and PrintStream are extremely similar in functionality and usage.

OutputStreamWriter is a bridge between OutputStream and Writer. Its subclass FileWriter is actually a specific class that implements this function (you can study SourceCode ). Functions and usage are extremely similar to those of OutputStream.

(6). Correspondence between input and output of the livestream

(7). byte stream conversion

The conversion stream serves as a bridge between the character stream and the word throttling.

You can convert the byte data to a character through specified encoding.

Character data can be converted to bytes by specified encoding.

When to use a conversion stream?

When there is a conversion between byte and character;

When the stream operation data needs to be encoded or decoded.

Specific Object embodiment:

InputStreamReader: a bridge between byte and character

OutputStreamWriter: a bridge between character and byte

These two stream objects are members of the character system. They have a conversion function, and they are character streams. Therefore, byte stream objects need to be input during construction.

(8). File class

A File class is an object that encapsulates files and folders in a File system. You can use the object idea to operate files and folders. File class stores various metadata information about a File or directory, including the File name, File length, last modification time, whether the File is readable, and obtaining the path name of the current File, determine whether a specified file exists, obtain the file list in the current directory, and create or delete files and directories.

(9). RandomAccessFile class

This object is not a member of the stream system. It encapsulates the byte stream and a buffer (character array) to operate data in the character array through internal pointers. This object features: This object can only operate on files, so the constructor receives two types of parameters: a. String File path and B. File object.

This object can both read and write files. You can specify the operation mode (r, rw) when instantiating objects)

Note: When the object is instantiated, if the file to be operated does not exist, it will be automatically created. If the file exists and the write data is not specified, it will be written from the beginning, that is, overwrite the original content. It can be used to download data in multiple threads or write data to files simultaneously.

2. Common java. io classes

All stream classes provided by DK are located in java. io packages and are inherited from the following four abstract stream classes.

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 ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.