java.lang.Objectjava.io.Reader
java.io.InputStreamReader
public class InputStreamReader
InputStreamReader 是字节流通向字符流的桥梁:它使用指定的 读取字节并将其解码为字符。它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集。 charset
每次调用 InputStreamReader 中的一个 read() 方法都会导致从底层输入流读取一个或多个字节。要启用从字节到字符的有效转换,可以提前从底层流读取更多的字节,使其超过满足当前读取操作所需的字节。
为了达到最高效率,可要考虑在 BufferedReader 内包装 InputStreamReader。例如:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedReader,
InputStream,
Charset
| 字段摘要 |
|---|
| 从类 java.io.Reader 继承的字段 |
|---|
lock |
| 构造方法摘要 | |
|---|---|
InputStreamReader(InputStream in) 创建一个使用默认字符集的 InputStreamReader。 |
|
InputStreamReader(InputStream in, Charset cs) 创建使用给定字符集的 InputStreamReader。 |
|
InputStreamReader(InputStream in, CharsetDecoder dec) 创建使用给定字符集解码器的 InputStreamReader。 |
|
InputStreamReader(InputStream in, String charsetName) 创建使用指定字符集的 InputStreamReader。 |
|
| 方法摘要 | |
|---|---|
void |
close() 关闭该流并释放与之关联的所有资源。 |
String |
getEncoding() 返回此流使用的字符编码的名称。 |
int |
read() 读取单个字符。 |
int |
read(char[] cbuf, int offset, int length) 将字符读入数组中的某一部分。 |
boolean |
ready() 判断此流是否已经准备好用于读取。 |
| 从类 java.io.Reader 继承的方法 |
|---|
mark, markSupported, read, read, reset, skip |
| 从类 java.lang.Object 继承的方法 |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| 构造方法详细信息 |
|---|
public InputStreamReader(InputStream in)
in - InputStream
public InputStreamReader(InputStream in, String charsetName) throws UnsupportedEncodingException
in - InputStream
charsetName - 受支持的
charset 的名称
UnsupportedEncodingException - 如果不支持指定的字符集
public InputStreamReader(InputStream in, Charset cs)
in - InputStream
cs - 字符集
public InputStreamReader(InputStream in, CharsetDecoder dec)
in - InputStream
dec - 字符集解码器
| 方法详细信息 |
|---|
public String getEncoding()
如果该编码有历史上用过的名称,则返回该名称;否则返回该编码的规范化名称。
如果使用 InputStreamReader(InputStream, String) 构造方法创建此实例,则返回的由此编码生成的唯一名称可能与传递给该构造方法的名称不一样。如果流已经关闭,则此方法将会返回 null。
null
Charset
public int read()
throws IOException
IOException - 如果发生 I/O 错误
public int read(char[] cbuf,
int offset,
int length)
throws IOException
cbuf - 目标缓冲区
offset - 从其处开始存储字符的偏移量
length - 要读取的最大字符数
IOException - 如果发生 I/O 错误
public boolean ready()
throws IOException
IOException - 如果发生 I/O 错误
public void close()
throws IOException