The difference between InputStream and InputStreamReader is that InputStream reads as byte, while InputStreamReader reads as char. For example, if the text in a file is abc,then both of them work fine. But if the text is a你们, which is composed of an a and two Chinese characters, then the InputStream does not work.
So we should use InputStreamReader, but my question is:
How does InputStreamReader recognize characters?
a is one byte, but a Chinese character is two bytes. Does it read a as one byte and recognize the other of characters as two bytes, or for every character in this text, does the InputStreamReader read it as two bytes?
An
InputStreamreads raw octet (8 bit) data. In Java, thebytetype is equivalent to thechartype in C. In C, this type can be used to represent character data or binary data. In Java, thechartype shares greater similarities with the Cwchar_ttype.An
InputStreamReaderthen will transform data from some encoding into UTF-16. If “a你们” is encoded as UTF-8 on disk, it will be the byte sequence61 E4 BD A0 E4 BB AC. When you pass theInputStreamtoInputStreamReaderwith the UTF-8 encoding, it will be read as the char sequence0061 4F60 4EEC.The character encoding API in Java contains the algorithms to perform this transformation. You can find a list of encodings supported by the Oracle JRE here. The ICU project is a good place to start if you want to understand the internals of how this works in practice.
As Alexander Pogrebnyak points out, you should almost always provide the encoding explicitly.
byte-to-charmethods that do not specify an encoding rely on the JRE default, which is dependent on operating systems and user settings.