简体   繁体   中英

What's the difference Between Character Stream and Byte Stream in Java and Char vs Byte in C?

在java中,人们说输入流逐字节读取文件,然后使用缓冲读取器将它们更改为characterstream.But在C char中引用字节(8位)。然后我们称之为java中的字符和字节。

In Java a byte is a signed 8-bit value, and a char is an unsigned 16-bit value. The Character is both a wrapper type for char and a utility class for a number of useful method supporting char

The key difference between an InputSTream is that it reads binary data, one byte at a time. A Reader is for reading text and it decodes bytes into char using the character encoding you set or the default encoding eg UTF-8 can turn 1, 2 or 3 bytes into a single char .

I suggest you learn more about the very basics of Java. It will save you a lot of time with these sort of questions.

For the C/C++ part, in those languages, a char is guaranteed to be at least 8 bits, so a char is at least as wide as a byte. I have been coding C since 1990 and C++ since 1992, and I have never seen a real platform/compiler combination where char and byte are not equivalent.

Also note that the other integer types are signed unless otherwise specified (eg 'int' is a signed integer), but 'char' is equivalent to 'unsigned char'.

A stream is a way of sequentially accessing a file. In Streams you can process the data one at a time as bulk operations are unavailable with them. But, streams supports a huge range of source and destinations including disk file, arrays, other devices, other programs etc. In Java, a byte is not the same thing as a char . Therefore a byte stream is different from a character stream. So, Java defines two types of streams: Byte Streams and Character Streams .

Byte Streams

A byte stream access the file byte by byte. Java programs use byte streams to perform input and output of 8-bit bytes. It is suitable for any kind of file, however not quite appropriate for text files. For example, if the file is using a unicode encoding and a character is represented with two bytes, the byte stream will treat these separately and you will need to do the conversion yourself. Byte oriented streams do not use any encoding scheme while Character oriented streams use character encoding scheme(UNICODE). All byte stream classes are descended from InputStream and OutputStream .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM