Java I/O-sorts out various "streams" and java

Source: Internet
Author: User

Java I/O-sorts out various "streams" and java
Background

Java. io provides a comprehensive IO Interface. Including file read/write and standard device output. In Java, IO isStreamFor input and output, all data is serialized into the output stream or read from the input stream. -- Baidu encyclopedia
Java. io has many classes. How can we clearly know when to use them?

Target

The goal of this article is to sort out the relationships between classes in java. io and understand the application scenarios of each class.

Concept

  Data Source,Data Stream,Destination. When you look at the Code related to java io, you need to ask yourself who each of the three points is. This article focuses on data streams.

Category

Classify various "streams" into different classes based on different classification methods ..

Although a variety of "streams" can be separated from different perspectives, they are not independent of each other and may overlap, just as FileInputStream is an input stream, also a byte stream or a node stream.
Differentiate the input stream from the output stream. Check whether the class name contains In, Out, or R or W. Distinguish between byte streams and bytes streams. Basically, it can be seen that the end is Stream, Reader, or Writer. So how to distinguish between the node stream and the processing stream? Here we will talk about the decorator mode in the design mode.
As mentioned above, there are three types of classification: crossover, And the byte stream contains both node streams and processing streams. The same is true for ghost streams. Here we use byte stream as an example to describe the relationship between the node stream and the processing stream in the decorator mode.
When learning this part, we compare the data stream to a pipe. If the node stream is directly connected to the pipe on the data source or destination, the processing stream is the pipe on the pipe, as shown in.

 

Decorator mode and Java IO

The modifier mode dynamically attaches the responsibility to the object to expand the behavior of the object. We can use the processing stream on the node stream as needed for layer-by-layer decoration. This is flexible and avoids class explosion through combination ". However, various "stream" classes also make it difficult for us to use APIs. But once you understand the Paster mode, you can pack it as needed.

Common usage
Node stream Purpose
FileInputStream Read data from a file
ByteArrayInputStream Read data from the byte array in memory
Stream Processing Purpose Add behavior/Remarks
BufferedInputStream The processing stream with a buffer zone avoids access to the hard disk once every byte read, increases the access rate, and reduces damage to the hard disk. For example, readline ()
DataInputStream Convert bytes directly to various basic types (int, double ......) Read For example, readdouble ()
ObjectInputStream Convert bytes directly into objects for reading Serializable, transient, externalizable
InputStreamReader Converts a byte to a character for reading. If a Chinese character is read by byte, you do not know what it is after reading it. --
Summary

The article does not list all input and output streams in java io. What is important is that, based on the above summary, we can draw a line between them. For more information, see the API documentation.

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.