简体   繁体   English

在Java中将字符存储为单个字节

[英]Storing characters as single bytes in java

I read that we should use Reader/Writer for reading/writing character data and InputStream/OutputStream for reading/writing binary data. 我读到我们应该使用Reader / Writer读取/写入字符数据,并使用InputStream / OutputStream读取/写入二进制数据。 Also, in java characters are 2 bytes. 另外,在Java中,字符为2个字节。 I am wondering how the following program works. 我想知道以下程序如何工作。 It reads characters from standard input stores them in a single byte and prints them out. 它从标准输入中读取字符,并将其存储在单个字节中并打印出来。 How are two byte characters fitting into one byte here? 两个字节字符如何适合一个字节?

http://www.cafeaulait.org/course/week10/06.html http://www.cafeaulait.org/course/week10/06.html

The comment explains it pretty clearly: 该评论非常清楚地说明了这一点:

// Notice that although a byte is read, an int
// with value between 0 and 255 is returned.
// Then this is converted to an ISO Latin-1 char 
// in the same range before being printed.

So basically, this assumes that the incoming byte represents a character in ISO-8859-1. 因此,基本上,这假设传入的字节代表ISO-8859-1中的字符。

If you use a console with a different encoding, or perhaps provide a character which isn't in ISO-8859-1, you'll end up with problems. 如果你使用一个控制台不同的编码,或者提供一个字符是不是 ISO-8859-1,你就会有问题的结束。

Basically, this is not good code. 基本上,这不是好的代码。

Java stores characters as 2 bytes, but for normal ASCII characters the actual data fits in one byte. Java将字符存储为2个字节,但是对于普通的ASCII字符,实际数据适合一个字节。 So as long as you can assume the file being read there is ASCII then that will work fine, as the actual numeric value of the character fits in a single byte. 因此,只要您可以假定正在读取的文件是ASCII,那么它将正常工作,因为字符的实际数字值适合单个字节。

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

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