Javase (i) IO class hierarchy relationship and usage summary of various IO streams

Source: Internet
Author: User

Today, I summarize this point of Io flow, because when I used the IO stream, I knew only a few of the most commonly used IO classes, such as Fileinputstream,bufferedinputstream (buffer stream), etc. But I do not know where it is in the entire IO system, for the whole IO system is not understood, each use of the time need to Baidu to see the use of examples, which means that their knowledge of this piece is deficient, so today, to detailed analysis, anatomy of the entire structure of IO.

Learn from Blog: http://www.cnblogs.com/skywang12345/p/io_01.html The blogger writes very well, his own re-write again is because want to follow their own ideas to summarize, convenient to review their own later. There is a certain similarity. Thank you

--wh

I. Classification of IO streams                                

1.1, according to the direction of data flow into the input stream , output stream

This input and output is relative to the code program we write,

Input stream: Get resources into our program from somewhere else (local files, resources on the network, etc.)

Output stream: Output stream is required to export from our program to another place (local file) and to save a string to a local file.

1.2, according to the processing data units are divided into byte stream , character stream

1 characters = 2 bytes, 1 bytes (byte) = 8 bits (bit), one kanji is two bytes in length

       BYTE stream: Every time read (write out) a bytes, when the transferred resource file is in Chinese, there will be garbled,

Character stream: Each read (write out) two bytes, when there is Chinese, the use of this stream can be transferred correctly display Chinese.

1.3, according to different functions divided into node flow , processing flow

       Node stream: Reads and writes data from or to a specific place (node). such as FileInputStream

processing Flow: Is the connection and encapsulation of an existing stream, which implements the data read and write through the functional invocation of the encapsulated stream. such as BufferedReader. The construction of the process flow always takes an additional stream object to do the argument. A Stream object passes through multiple wrappers of other streams ,

This is where it comes in. Decorator Mode , which will explain the pattern in IO in detail later.

1.4, 4 basic abstract stream types, all streams inherit these four.

Input stream output stream

byte stream InputStream OutputStream

Character Stream Reader Writer

InputStream: Byte input stream

OutputStream: Byte output stream

Reader: Character input stream

Writer: Character output stream

     

    1.5. Summary Flow classification

Look at a few of the above categories, may be for the first time learning IO students feel a bit confused, when to use the word stream, when to use the output stream? It's really very simple, for example, to learn,

1, first of all to know whether to choose the input stream or output stream, which depends on their own situation, if you want to write something from the program to another place, then select the output stream, and vice versa with the input stream

2, and then consider when you transfer data, is the choice of using a byte stream or a character stream, that is, each time 1 bytes or 2 bytes, Chinese is definitely selected character stream.

3, the first two steps can be selected a suitable node stream, such as byte input stream InputStream, if you want to enhance the function on this basis, then in the processing stream select a suitable.

Analysis of inheritance relationship of 4 basic abstract classes                            

     2.1, InputStream byte input stream

          

2.1.1, understanding the function of each class is the role

Just get to know it now, and the detailed features will be written in the blog that follows.

Bytearrayinputstream: byte array input stream, the function of which is to read in bytes from a byte array (byte[]), that is, the resource file is stored in bytes in the form of a byte array in the class, We take it from this byte array, too.

PipedInputStream: Pipeline byte input stream, which is used in conjunction with PipedOutputStream to enable pipeline communication between multiple threads

FilterInputStream: Decorator mode is in the decorator, the specific decorator will inherit it, so under the subclass of this class is used to decorate the other flow, that is, the processing class. The decorator pattern will be explained below, and then it will be understood.

Bufferedinputstream: Buffer stream, decorate the processing flow, enhance, there will be a buffer, for storing bytes, each time the buffer is full and then sent, instead of a byte or two bytes to send. Higher efficiency

DataInputStream: Data input stream, which is used to decorate other input streams, which "allows applications to read basic Java data types from the underlying input stream in a machine-independent manner"

Fileinputsream: file input stream. It is typically used to read files

File: Manipulate the files in the specified directory to see the blog post explaining file. Note that this class is not inherited from the four basic classes, although it is under the IO package.

ObjectInputStream: An object input stream used to provide persistent storage of "basic data or objects". Popular point, that is, can directly transfer objects,

      2.2, OutputStream byte output stream

              

The specific analysis has been analyzed above, but with InputStream is relative, but the function is the same.

      2.3. Reader character input stream

                

The function is similar to the same. Analyze one here.

InputStreamReader: The conversion stream converts a byte input stream into a character input stream. It is a bridge of bytes flowing to a character stream

      2.4. Writer character output stream

                  

third, the use of the decorator model                                    

      3.1. What is decorator mode?

Recommend a blog post, http://blog.csdn.net/jason0539/article/details/22713711 in detail what is decorator mode? In my own words, is to add more features, and we first think of inheritance, inheritance is very good in line with our requirements, no matter how many layers you want to add functionality, you can use the inheritance layer of implementation, but this brings a problem, once I need to change my needs, Then I need to change things in the source code, and then in this inheritance chain in a certain class to do some modification, this does not conform to our design pattern thinking, so there is the decorator pattern, the decorator has been decorated by the example, and then what the specific decoration we all write another class to inherit the decorator, when we need the decoration, The class is then passed in as a parameter by the decorator.

Intervention may not understand so clearly, now look at a concrete example. For example, we need to make a chicken leg fort, what is the process? See

1, the first basic raw materials, that is, two pieces of bread, this is no matter what hamburgers are needed,

2, what to do hamburger, depending on what materials to add, such as lettuce, chicken, etc., so according to the material to make hamburgers, what to do hamburger to add material

3. After all materials are added, the price can be calculated directly.

Is it much more convenient to use the decorator mode than to always use inheritance? To change a hamburger, do not need to change the source code, do not need anything, I hope you can understand the ideas.

                    

      3.2. Application of decorator mode in IO stream

Draw a picture, in combination with the source code and write their own codes to see.

                  

Here, it should be possible to understand the previous processing flow and node flow, in fact, the processing flow is a concrete decorator, and the node flow is the decorator.

Iv. Summary

4.1, should be aware of the various types of IO flow of the structure of the relationship, usually we use the IO class is in what position, is a what kind of flow (node flow or processing flow, input stream or output stream, byte stream or character stream)

4.2. Application of decorator mode in IO stream and what is decorator mode

4.3, why the IO stream will be written in that format, the instance of one stream into the construction method of another stream?

Javase (i) IO class hierarchy relationship and usage summary of various IO streams

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.