类 InputStreamReader

来源:互联网 发布:程序员专业名词 编辑:程序博客网 时间:2024/05/16 12:06

类 InputStreamReader           原文链接:http://www.cnblogs.com/springside5/archive/2012/05/06/2486246.html

java.lang.Object

  java.io.Reader

      java.io.InputStreamReader

所有已实现的接口:

CloseableReadable

直接已知子类:

FileReader


public class InputStreamReader

extends Reader

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

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

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

 BufferedReader in

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

 

从以下版本开始:

JDK1.1

另请参见:

BufferedReaderInputStreamCharset


字段摘要

 

从类 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 继承的方法

markmarkSupportedreadreadresetskip

 

从类 java.lang.Object 继承的方法

cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

 

构造方法详细信息

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,

                        Charset cs)

创建使用给定字符集的InputStreamReader。

参数:

in - InputStream

cs - 字符集

从以下版本开始:

1.4


InputStreamReader

public InputStreamReader(InputStream in,

                        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,

                intoffset,

                intlength)

         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

从类 Reader复制的描述

关闭该流并释放与之关联的所有资源。在关闭该流后,再调用 read()、ready()、mark()、reset() 或 skip() 将抛出IOException。关闭以前关闭的流无效。

指定者:

接口 Closeable 中的 close

指定者:

类 Reader 中的 close

抛出:

IOException - 如果发生 I/O 错误

0 0
原创粉丝点击