InputStream vs InputStreamReader

前端 未结 6 1444
暗喜
暗喜 2020-12-12 14:43

What\'s the benefit of using InputStream over InputStreamReader, or vice versa.

Here is an example of InputStream in action:

6条回答
  •  温柔的废话
    2020-12-12 15:39

    They represent somewhat different things.

    The InputStream is the ancestor class of all possible streams of bytes, it is not useful by itself but all the subclasses (like the FileInputStream that you are using) are great to deal with binary data.

    On the other hand, the InputStreamReader (and its father Reader) are used specifically to deal with characters (so strings) so they handle charset encodings (utf8, iso-8859-1, and so on) gracefully.

    The simple answer is: if you need binary data you can use an InputStream (also a specific one like a DataInputStream), if you need to work with text use an InputStreamReader..

提交回复
热议问题