JavaTM 2 Platform
Standard Ed. 5.0

java.io
类 InputStreamReader

java.lang.Object
  继承者 java.io.Reader
      继承者 java.io.InputStreamReader
所有已实现的接口:
Closeable, Readable
直接已知子类:
FileReader

public class InputStreamReader
extends Reader

InputStreamReader 是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。它使用的字符集可以由名称指定或显式给定,否则可能接受平台默认的字符集。

每次调用 InputStreamReader 中的一个 read() 方法都会导致从基础输入流读取一个或多个字节。要启用从字节到字符的有效转换,可以提前从基础流读取更多的字节,使其超过满足当前读取操作所需的字节。

为了达到最高效率,可要考虑在 BufferedReader 内包装 InputStreamReader。例如:

 BufferedReader in
   = new BufferedReader(new InputStreamReader(System.in));
 

从以下版本开始:
JDK1.1
另请参见:
BufferedReader, InputStream, Charset

字段摘要
 
从类 java.io.Reader 继承的字段
lock
 
构造方法摘要
InputStreamReader(InputStream in)
          创建一个使用默认字符集的 InputStreamReader。
InputStreamReader(InputStream in, java.nio.charset.Charset cs)
          创建使用给定字符集的 InputStreamReader。
InputStreamReader(InputStream in, java.nio.charset.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
 

构造方法详细信息

InputStreamReader

public InputStreamReader(InputStream in)
创建一个使用默认字符集的 InputStreamReader。

参数:
in - InputStream

InputStreamReader

public InputStreamReader(InputStream in,
                         String charsetName)
                  throws UnsupportedEncodingException
创建使用指定字符集的 InputStreamReader。

参数:
in - InputStream
charsetName - 受支持的 charset 的名称
抛出:
UnsupportedEncodingException - 如果不支持指定的字符集

InputStreamReader

public InputStreamReader(InputStream in,
                         java.nio.charset.Charset cs)
创建使用给定字符集的 InputStreamReader。

参数:
in - InputStream
cs - 字符集
从以下版本开始:
1.4

InputStreamReader

public InputStreamReader(InputStream in,
                         java.nio.charset.CharsetDecoder dec)
创建使用给定字符集解码器的 InputStreamReader。

参数:
in - InputStream
dec - 字符集解码器
从以下版本开始:
1.4
方法详细信息

getEncoding

public String getEncoding()
返回此流使用的字符编码的名称。

如果该编码有历史上用过的名称,则返回该名称;否则返回该编码的规范化名称。

如果使用 InputStreamReader(InputStream, String) 构造方法创建此实例,则返回的由此编码生成的唯一名称可能与传递给该构造方法的名称不一样。如果流已经关闭,则此方法可能返回 null

返回:
此编码的历史名称,如果流已经关闭,则可能返回 null
另请参见:
Charset

read

public int read()
         throws IOException
读取单个字符。

覆盖:
Reader 中的 read
返回:
读取的字符,如果已到达流的末尾,则返回 -1
抛出:
IOException - 如果发生 I/O 错误

read

public int read(char[] cbuf,
                int offset,
                int length)
         throws IOException
将字符读入数组中的某一部分。

指定者:
Reader 中的 read
参数:
cbuf - 目标缓冲区
offset - 以其处开始存储字符的偏移量
length - 要读取的最大字符数
返回:
读取的字符数,如果已到达流的末尾,则返回 -1
抛出:
IOException - 如果发生 I/O 错误

ready

public boolean ready()
              throws IOException
告知是否准备读取此流。如果其输入缓冲区不为空,或者可从基础字节流读取字节,则 InputStreamReader 为已做好被读取准备。

覆盖:
Reader 中的 ready
返回:
如果保证下一个 read() 不阻塞输入,则返回 True,否则返回 false。注意,返回 false 并不保证阻塞下一次读取。
抛出:
IOException - 如果发生 I/O 错误

close

public void close()
           throws IOException
关闭该流。

指定者:
接口 Closeable 中的 close
指定者:
Reader 中的 close
抛出:
IOException - 如果发生 I/O 错误

JavaTM 2 Platform
Standard Ed. 5.0

提交错误或意见
有关更多的 API 参考资料和开发人员文档,请参阅 Java 2 SDK SE 开发人员文档。该文档包含更详细的、面向开发人员的描述,以及总体概述、术语定义、使用技巧和工作代码示例。

版权所有 2004 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策