Java InputStreamReader

2024 年 8 月 29 日 | 阅读 2 分钟

InputStreamReader 是字节流到字符流的桥梁:它读取字节并使用指定的字符集将它们解码为字符。它可以按名称指定或显式给出它使用的字符集,或者接受平台的默认字符集。

构造函数

构造函数名称描述
InputStreamReader(InputStream in)创建一个使用默认字符集的 InputStreamReader。
InputStreamReader(InputStream in, Charset cs)创建一个使用给定字符集的 InputStreamReader。
InputStreamReader(InputStream in, CharsetDecoder dec)创建一个使用给定字符集解码器的 InputStreamReader。
InputStreamReader(InputStream in, String charsetName)创建一个使用指定字符名的 InputStreamReader。

方法

修饰符和类型方法描述
voidclose()它关闭流并释放与其关联的任何系统资源。
StringgetEncoding()返回此流使用的字符编码的名称。
intread()它读取单个字符。
intread(char[] cbuf, int offset, int length)将字符读入数组的一部分。
booleanready()它指示此流是否已准备好读取。

示例

输出

I love my country

The file.txt contains text "I love my country" the InputStreamReader 
reads Character by character from the file