Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the difference between a byte stream and a character stream in Java

Shulou Source: shulou.com Published: 2022-06-03 11:15:18 10月03日 Update

This article mainly explains "what is the difference between Java byte stream and character stream". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the difference between Java byte stream and character stream".

What is the difference between a Java basic byte stream and a character stream? To output a piece of binary data to a device one by one, or to read a piece of binary data one by one from a device, no matter what the input and output device is, we need to complete these operations in a unified way and describe them in an abstract way, which is called IO stream, and the corresponding abstract classes are OutputStream and InputStream. Different implementation classes represent different input and output devices. They all operate on bytes.

In applications, a piece of text that is entirely a character is often output or read in. Is it possible to use a byte stream? Everything in the computer ultimately exists in the form of binary bytes. For "China" characters, first get their corresponding bytes, and then write the bytes to the output stream. When reading, the first thing to read is a byte, but to display it as a character, we need to convert the byte to a character.

Because of the wide range of such requirements, a wrapper class for character streams is specially provided. The underlying device always accepts only byte data, and sometimes to write a string to the underlying device, you need to convert the string into bytes before writing. The character stream is the wrapper of the byte stream, while the character stream accepts the string directly, which internally converts the string into bytes and then writes it to the underlying device, which makes it a little convenient for us to write or read strings to the IO.

When converting characters to bytes, you should pay attention to the problem of coding, because when a string is converted into a byte array, it is actually converted into a byte form of some encoding of the character, and reading is also vice versa.

Thank you for your reading, the above is the content of "what is the difference between Java byte stream and character stream". After the study of this article, I believe you have a deeper understanding of what is the difference between Java byte stream and character stream. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Characters bytes devices strings output data binaries ways learning differences content underlying form coding problems packaging input representation basics that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Information NVidia MariaDB Microsoft