今天在做项目时,最终接触到了我不怎么熟悉的IO方面的知识——IO流。
这次就在InputStream这个类上出了问题,首先看看官方对InputStream的一些介绍:
This abstract class is the superclass of all classes representing an input stream of bytes.
Applications that need to define a subclass of InputStream
must always provide a method that returns the next byte of input.
意思是说,这个类是一切表示字节输入流的父类,需要定义一个inputStream的子类一定要提供一个返回输入的下一个字节,也就是说,InputStream是字节输入流的根本。它是一次读出一个字节,但是读出的字节是一种int,需要通过转换手段转换成char字符
再来看InputStreamReader:
An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified
. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.charset
Each invocation of one of an InputStreamReader's read() methods may cause one or more bytes to be read from the underlying byte-input stream. To enable the efficient conversion of bytes to characters, more bytes may be read ahead from the underlying stream than are necessary to satisfy the current read operation.
For top efficiency, consider wrapping an InputStreamReader within a BufferedReader. For example:
BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));
意思是说,inputStreamReader是一个针对字节流的到字符流的桥梁类,用这个类就能将字节流中转化成我们想要看到的字符了。
所以,在Java中字节流与字符流需要理清楚,不能误认为字节流就是能够看到的字符。