Talking about the concept and application of Java stream

Source: Internet
Author: User
Tags serialization

All of Java's I/O mechanisms are input and output based on data streams that represent the flow sequences of character or byte data. Java's I/O stream provides a standard way to read and write data. Any object in Java that represents a data source provides a way to read and write its data in a data stream.

Standard input and output, file operations, data streams on the network, string streams, object streams, zip file streams, and so on, the input and output abstraction in Java is called a stream, like a water pipe, connecting two containers together. reading data into memory (for use by Java programs) in external memory is called an input stream, and data is written from memory into external memory called the output stream.

Flow is a very image concept, when the program needs to read the data, it will open a stream to the data source, the data source can be a file, memory, or network connection. Similarly, when a program needs to write data, it opens a stream to the destination.

A stream is a set of sequences of bytes that have a starting point and an end point, a generic or abstract of the data transfer. That is, the transmission of data between the two devices is called the flow, the essence of the flow is data transmission, according to the data transmission characteristics of the stream abstracted into various classes, convenient and more intuitive data manipulation.

Classification of streams:

Stream classification, Java Stream classification is rich, just contact people will feel dizzy after seeing. There are many ways to categorize streams:

1, according to the direction of input, input stream and output stream, the input and output of the reference object is a Java program.

2, according to the processing data units different points, byte stream and character stream, the byte stream read the smallest unit is a bytes (1byte=8bit), and the character stream can read one character at a time (1char = 2byte = 16bit).

3, according to the different points of the function, sub-node flow and processing flow, the node flow is directly from a source read and write data stream (this stream has not been wrapped and modified), processing flow is on the basis of the node flow encapsulation of a stream, FileInputStream is a node stream, you can read data directly from the file, But the Bufferedinputstream can be packaged with FileInputStream, making it a buffer function.

Class structure diagram of the stream:

The concept and introduction of "Flow" in Java

Byte stream and character stream:

The origin of a character stream: Because of the difference in data encoding, there is a stream object that efficiently operates on characters. The essence is actually based on the byte stream reading, to check the specified code table. The difference between a byte stream and a character stream:

Read-write units are different: byte stream in bytes (8bit), character stream in characters, according to the Code table mapping characters, one can read multiple bytes at a time.

The processing object is different: The byte stream can handle all types of data (slices, AVI, etc.), while the character stream can only handle data of the type of characters.

Conclusion: As long as you are working with plain text data, you will prefer to use character streams. In addition, byte streams are used.

One thing to mention here is that the length of an English character is 1byte, and the length of the Chinese character is 2byte. If we combine the Chinese and English mixed documents in accordance with the word stream processing, there will be fragmentation confusion, garbled. This is, we need to use a character stream to handle. That's why you put InputStream into FileReader when you're working on a document.

Direction:

The stream is divided into the input stream and the output stream. But it's all relative. Especially in the case of Hadoop serialization, it is not always known whether to use an input stream or an output stream. Here, we can make a distinction:

Serialization is the processing of data by byte stream. The byte stream is just a form of storage. He is not recognized by the program and needs to deserialize the byte stream into a Java-recognized object before it can be recognized by a Java program. So, serialization is word throttling, which is output from Java code or memory to external memory. The deserialization is the object, that is, from the external memory to convert the byte stream data into the program recognition object.


Welcome to join the Learning Exchange Group 569772982, we learn to communicate together.

Talking about the concept and application of Java stream

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.