java Java中的字符流和字节流以及C中的Char vs Byte有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32579881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference Between Character Stream and Byte Stream in Java and Char vs Byte in C?
提问by priwiljay
In java people say inputstream reads a file byte by byte then using buffered reader they change to characterstream.But in C char refer to byte(8 bit).Then what we call as character and byte in java.
在java中人们说输入流逐字节读取文件,然后使用缓冲读取器将它们更改为字符流。但在C中,char指的是字节(8位)。然后我们在java中称为字符和字节。
回答by Peter Lawrey
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
在 Java 中,abyte
是有符号的 8 位值,而 achar
是无符号的 16 位值。的Character
是既为一个包装型char
和实用类为一些有用的方法的支持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 e.g. UTF-8
can turn 1, 2 or 3 bytes into a single char
.
InputSTream 之间的主要区别在于它一次读取一个字节的二进制数据。AReader
用于读取文本,它char
使用您设置的字符编码或默认编码将字节解码为例如UTF-8
可以将 1、2 或 3 个字节转换为单个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.
我建议您更多地了解 Java 的基础知识。回答这些问题会为您节省大量时间。
回答by Erik Alap??
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.
对于 C/C++ 部分,在这些语言中,char 保证至少为 8 位,因此 char 至少与字节一样宽。我从 1990 年开始编写 C,从 1992 年开始编写 C++,我从未见过真正的平台/编译器组合,其中 char 和 byte 是不等价的。
Also note that the other integer types are signed unless otherwise specified (e.g. 'int' is a signed integer), but 'char' is equivalent to 'unsigned char'.
另请注意,除非另有说明,否则其他整数类型是有符号的(例如,'int' 是有符号整数),但 'char' 等效于 'unsigned char'。
回答by shailesh kumar
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 .
流是一种顺序访问文件的方式。在 Streams 中,您可以一次处理一个数据,因为它们无法进行批量操作。但是,流支持大量的源和目标,包括磁盘文件、数组、其他设备、其他程序等。在 Java 中,字节与字符不同。因此字节流不同于字符流。因此,Java 定义了两种类型的流:字节流和字符流。
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 .
字节流逐字节访问文件。Java 程序使用字节流来执行 8 位字节的输入和输出。它适用于任何类型的文件,但不太适合文本文件。例如,如果文件使用 unicode 编码并且一个字符用两个字节表示,则字节流将分别处理它们,您需要自己进行转换。面向字节的流不使用任何编码方案,而面向字符的流使用字符编码方案(UNICODE)。所有字节流类都来自 InputStream 和 OutputStream 。