Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Java byte stream and character stream

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to understand Java byte stream and character stream, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Let's start with today's content.

Byte streams and character streams are actually easy to understand!

A byte stream is a Byte stream, and a byte is Byte.

The character stream is the Character stream, and the character is Character.

There are two unit conversions here that you need to remember.

1 byte = 8 bit

1 char = 2 byte = 16 bit

Although 1 bit is the true smallest unit of data, 1 bit has too little information. To represent a useful information, you need several bit to express it together. So in most cases, bytes are the smallest basic unit of data.

For example, the size of the well-known basic model is an integral multiple of 8 bit (that is, 1 byte):

Boolean: 1 byte

Short: 2 byte

Int: 4 byte

Float: 4 byte

Long: 8 byte

Double: 8 byte

So far, all we know is bytes. However, the Java IO library provided by the Java Imax O coding system has two branches, byte stream-oriented InputStream and OutputStream; character-oriented Reader and Writer.

Why should there be a character stream? That's because in English, we can express it in bytes, but Chinese, Japanese, Korean and so on can't be expressed in bytes, so all people think of a new character coding set. For example, Unicode character set, GB 18030 Magi GBK Magi Big5 Magi ISO Murray 8859-1, etc.

So, in the end, a character is made up of 1, 2, or 4 bytes. All in all, everything is a byte stream, but there is no such thing as character stream. Characters are only the product of the translation of byte streams according to the code set.

Therefore, the InputStream and OutputStream of byte stream are the foundation of everything. The only thing that flows in the actual bus is byte stream. The byte stream needs to be specially decoded to get the character stream. In Java, the bridge responsible for decoding the character stream from bytes to characters is InputStreamReader and InputStreamWriter.

Look at the structure diagram of InputStreamReader and OutputStreamWriter below.

It is the StreamDecoder class and StreamEncoder class that are actually responsible for encoding and decoding. The character encoding set Charset used must be specified during the encoding process. So both InputStreamReader and OutputStreamWriter constructors take parameters of type Charset.

If no code set is specified, the system default code set is used. FileInputReader and FileOutputWriter, which we often use, are derived classes from InputStreamReader and OutputStreamWriter.

There is no character stream in the computer, at first it is only applicable to English, but the Internet belongs to all mankind, so people come up with the character stream again. Characters are just wrapped bytes.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report