我读到我们应该使用 Reader/Writer 来读取/写入字符数据,使用 InputStream/OutputStream 来读取/写入二进制数据。另外,在java中字符是2个字节。我想知道下面的程序是如何工作的。它从标准输入读取字符,将它们存储在单个字节中并将其打印出来。这里两个字节字符如何装入一个字节?
最佳答案
评论解释得很清楚:
// 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.
基本上,这假设传入的字节表示 ISO-8859-1 中的字符。
如果您使用具有不同编码的控制台,或者提供的字符可能不符合 ISO-8859-1,您最终会遇到问题。
基本上,这不是好的代码。
关于java - 在java中将字符存储为单个字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14635600/