[Java articles] Do you remember the knowledge about byte and character streams?

Table of contents

byte stream

character stream

The difference between byte stream and character stream


byte stream

InputStream / OutputStream

        A byte stream is a stream of data that is read in units of bytes. It is often used to process the input and output of binary data, such as keyboard input and network communication. But byte streams cannot display Unicode characters correctly.

input stream

InputStream in = new InputStream(socket.getIntputStream());        // 创建输入对象
int len = in.available();                                          // 读取输入对象长度
char c = (char)in.read();                                          // 读取输入字节
byte[] b = new byte[len];                                          // 连续读取输入字节
in.read(b);
in.close();                                                        // 关闭输入对象

output stream

OutputStream out = new OutputStream(socket.getOutputStream());     // 创建输出对象
byte[] b = {1,2,3};                                                // 导入输出字节          
out.write(b);
out.flush();                                                       // 刷新输出对象,输出字节
out.close();                                                       // 关闭输出对象,输出字节

character stream

Reader/Writer class

        A character stream is a data stream that is read in units of characters. Can only be used to process text data. And all text data, that is, Unicode-encoded data, must be presented in the form of a character stream.

We often need to use character streams to process data in Java programs, but we need to use byte streams in communication. This requires data format conversion.

InputStreamReader class

        Subclasses the Reader class. Converts byte stream data into a character stream, often used to read console input or read network traffic. The encoding method can be specified, otherwise the IDE default encoding method will be used.

// 读取键盘输入
InputStreamReader in = new InputStreamReader(System.in);
// 读取套接字通信,并指定编码格式
InputStreamReader in = new InputStreamReader(socket.getInputStream(), "UTF-8");

OutputStreamWriter class 

        Writer class subclass. Convert character stream data into a byte stream, often used to send network communications.

// 数据转化为字节流发送
OutputStreamWriter out = new OutputStreamWriter(socket.getOutputStream());

The difference between byte stream and character stream

1. The composition of the two is different: (This also directly affects the way we read data later)

                Composition of byte stream: Byte stream is composed of bytes.
                The composition of the character stream: The character stream is composed of characters.
2. Computers treat the two differently:

                Byte stream: It is mainly used to process binary data, which is processed in bytes, but in reality a lot of data is text.
                Character stream: It is processed according to the encode of the virtual machine, that is, character set conversion is required.

Note: Characters in Java are composed of two bytes. 1 character = 2 bytes

        In fact, we can understand it as character stream = byte stream + encoding table, why can we understand it this way, because byte stream read data adopts ASCII encoding by default, and ASCll encoding is mainly for data where one byte represents one character, However, our Chinese characters are one character = two bytes. If ASCll encoding is used to read Chinese characters, there will be garbled characters. In the byte stream, we have no way to change the use of other encoding types, but the character stream can, it uses UTF encoding. , supports Chinese, and we can also set other encoding types in the character stream object. The character stream is based on the byte stream and adds the function of selecting other encoding types.

Guess you like

Origin blog.csdn.net/m0_64231944/article/details/127977046