我读到我们应该使用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.
Run Code Online (Sandbox Code Playgroud)
所以基本上,这假设传入的字节代表ISO-8859-1中的一个字符.
如果你使用一个控制台不同的编码,或者提供一个字符是不是在 ISO-8859-1,你将有问题的结束.
基本上,这不是好的代码.
| 归档时间: |
|
| 查看次数: |
1424 次 |
| 最近记录: |