InputStream vs InputStreamReader

前端 未结 6 1462
暗喜
暗喜 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

    Well InputStreamReader is used to directly read characters.

    So reading them as int and then converting to char is not really optimal.

    That is the main difference I believe.

    InputStream gives you the bytes, and the InputStreamReader gives you already chars so it reads the InputStream 8bits at a time.

    In addition, if you're reading big chunks of text, you can even wrap the InputStreamReader in a BufferedReader which provides you with some nice methods to let's say read whole lines at once.

    This helping you out ?

    You can also read this article: https://docs.oracle.com/javase/tutorial/essential/io/charstreams.html

    Cheers,

提交回复
热议问题