Home > Java > Javagetting Started > What is the difference between java streams

What is the difference between java streams

爱喝马黛茶的安东尼
Release: 2019-11-14 09:36:48
Original
1830 people have browsed it

What is the difference between java streams

All data in the program is transmitted or saved in the form of streams. When the program needs data, it must use the input stream to read the data, and when the program needs some data When saving, it is done using the output stream.

The input and output in the program are all saved in the form of streams, and what is saved in the streams are actually byte files.

Byte stream and character stream

There are two main categories of operating file content in the java.io package: byte stream and character stream, both of which are Divided into input and output operations. Outputting data in a byte stream is mainly done using OutputStream, and input is done using InputStream. Outputting in a character stream is mainly done using the Writer class, and input streams are mainly done using the Reader class. (These four are abstract classes)

Java provides a package dedicated to input and output functions, Java.io, including:

InputStream, OutputStream, Reader, Writer

InputStream and OutputStream are designed for byte streams and are mainly used to process bytes or binary objects.

Reader and Writer are designed for character streams (one character occupies two bytes) and are mainly used to process characters or strings.

The character stream processing unit is a 2-byte Unicode character, operating on characters, character arrays or strings respectively, while the byte stream processing unit is 1 byte, operating on bytes and byte arrays. Therefore, the character stream is formed by the Java virtual machine converting bytes into 2-byte Unicode characters, so it has better support for multiple languages! If it is audio files, pictures, and songs, it is better to use byte streams. If it is related to Chinese (text), it is better to use character streams.

All files are stored in bytes. What is retained on the disk is not the characters of the file, but the characters are first encoded into bytes, and then these bytes are stored on the disk. When reading a file (especially a text file), it is also read byte by byte to form a byte sequence.

Byte stream can be used for any type of object, including binary objects, while character stream can only process characters or strings; 2. Byte stream provides the function of processing any type of IO operation, but it cannot Handles Unicode characters directly, and character streams do just that.

Byte stream is the most basic, all subclasses of InputStrem and OutputStream are, mainly used to process binary data, it is processed by bytes, but in reality a lot of data is text, and The concept of character stream is proposed, which is processed according to the encoding of the virtual machine, that is, the character set is converted. The two are related through InputStreamReader and OutputStreamWriter. In fact, they are related through byte[] and String. Chinese character problems that arise during development are actually caused by the inconsistency in conversion between character streams and byte streams.

The return type of the read() method of the Reader class is int: the character read as an integer (accounting for 16 bits in two bytes), ranging from 0 to 65535 (0x00-0xffff), if When the end of the stream is reached, -1 is returned.

Although inputStream's read() also returns int, since this class is oriented to byte streams and one byte occupies 8 bits, it returns an int byte value in the range of 0 to 255. If no bytes are available because the end of the stream has been reached, the value -1 is returned. Therefore, values ​​that cannot be represented by 0-255 must be read using a character stream! For example, Chinese characters.

php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template