Home  >  Article  >  Java  >  What is java stream

What is java stream

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-11-14 09:19:064370browse

What is java stream

1. Concept

File operations in Java are performed in a stream manner. A stream is an ordered sequence of data in Java memory. Java reads data from the source (file, memory, keyboard, network) into memory to form a stream, and then writes these streams to other destinations (file, memory, console, network), so it is called It is a stream because this data sequence operates on different parts of the source at different times.

2. Classification

Classification of streams, Java’s stream classifications are relatively rich, and people who are new to it will feel dizzy after reading it. There are many ways to classify streams:

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

2. According to the unit of processing data, it is divided into byte stream and character stream. The minimum unit of byte stream reading is one byte (1byte=8bit), while the character stream can read one character at a time. (1char = 2byte = 16bit).

3. According to different functions, it is divided into node flow and processing flow. Node flow is a flow that directly reads and writes data from a source (this flow is not packaged and modified), and processing flow is a flow that processes node flow. A stream based on encapsulation, FileInputStream is a contact stream that can read data directly from a file, but BufferedInputStream can wrap FileInputStream to make it have buffering function.

In fact, in addition to the above three categories, there are also some commonly heard categories such as: object stream, buffer stream, compressed stream, file stream, etc. In fact, they are all subcategories of node flows and processing flows. Of course you can also create new stream types if you need to.

3. The relationship between flow classification

No matter how rich and complex the flow classification is, its roots come from four basic categories. The relationship between these four classes is as follows:

Byte stream character stream

Input stream InputStream Reader

Output stream OutputStream Writer

IV. Mutual conversion of byte stream and character stream

1. From byte stream to character stream: InputStreamReader and OutputStreamWriter classes can achieve this.

2. From character stream to byte stream: You can get the char[] array from the character stream, convert it to String, and then call the String API function getBytes() to get the byte[], and then you can use ByteArrayInputStream, ByteArrayOutputStream to achieve conversion to byte stream.

Many java training videos, all on the PHP Chinese website, welcome to learn online!

The above is the detailed content of What is java stream. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to run java programNext article:How to run java program