简体   繁体   English

Java中的字符流和字节流有什么区别?C中的字符串和字节与字节有什么区别?

[英]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. 在Java中, byte是带符号的8位值, char是无符号的16位值。 The Character is both a wrapper type for char and a utility class for a number of useful method supporting char Character既是char的包装类型,也是支持char的许多有用方法的实用程序类

The key difference between an InputSTream is that it reads binary data, one byte at a time. InputSTream的主要区别在于它一次读取一个字节的二进制数据。 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 . Reader用于读取文本,它使用您设置的字符编码或默认编码将字节解码为char ,例如UTF-8可以将1,2或3个字节转换为单个char

I suggest you learn more about the very basics of Java. 我建议你更多地了解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. 对于C / C ++部分,在这些语言中,char保证至少为8位,因此char至少与字节一样宽。 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. 我从1990年开始编写C语言,自1992年开始编写C ++,我从未见过真正的平台/编译器组合,其中char和byte不相等。

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'. 另请注意,除非另行指定,否则其他整数类型都是有符号的(例如'int'是有符号整数),但'char'等效于'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. 在Streams中,您可以一次处理一个数据,因为批量操作不可用。 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 . 但是,流支持大量的源和目标,包括磁盘文件,数组,其他设备,其他程序等。在Java中,字节与char不同。 Therefore a byte stream is different from a character stream. 因此,字节流与字符流不同。 So, Java defines two types of streams: Byte Streams and Character Streams . 因此,Java定义了两种类型的流:字节流和字符流。

Byte 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. Java程序使用字节流来执行8位字节的输入和输出。 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. 例如,如果文件使用的是unicode编码,并且字符用两个字节表示,则字节流将单独处理这些字节,您需要自己进行转换。 Byte oriented streams do not use any encoding scheme while Character oriented streams use character encoding scheme(UNICODE). 面向字节的流不使用任何编码方案,而面向字符的流使用字符编码方案(UNICODE)。 All byte stream classes are descended from InputStream and OutputStream . 所有字节流类都来自InputStream和OutputStream。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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