IO-InputStreamReader

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。例如:

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

 

字段摘要

从类 java.io.Reader 继承的字段

lock

 

构造方法摘要

InputStreamReader(InputStream in)

         创建一个使用默认字符集的InputStreamReader。

InputStreamReader(InputStream in,Charset cs)

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

InputStreamReader(InputStream in,String charsetName)

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

InputStreamReader(InputStream in,CharsetDecoder dec)

         创建使用给定字符集额解码器的 INputStreamReader。

 

方法摘要

void  close()

关闭该流并释放与之关联的所有资源。

String  getEncoding()

返回此流使用的字符编码的名称。

Int  read()

读取单个字符,返回字符的ASCII码。

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, Charset cs)

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

参数:

in - InputStream

cs - 字符集

 

InputStreamReader

public InputStreamReader(InputStream in,CharsetDecoder dec)

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

参数:

in - InputStream

dec - 字符集解码器

 

方法详细信息

getEncoding

public String getEncoding()

返回此流使用的字符编码的名称。

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

 

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

 

如果流已经关闭,则此方法会返回null。

 

返回:

此编码的历史名称;如果流已经关闭,则返回null

 

read

public int read()throws IOException

读取单个字符。

覆盖:

类Reader中的read

返回:

读取的字符的ASCII码;如果已经到达流的末尾,则返回-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

从类Reader复制的描述

关闭该流并释放与之关联的所有资源。

在关闭该流后,在调用read(),ready(),mark(),reset()或skip()将抛出IOException。

关闭以前关闭的流无效。

指定者:

接口Closeable中的close

指定者:

类Reader中的close

抛出:

IOException – 如果发生I/O错误

 

 

posted @ 2015-07-27 16:18  程序员袍子  阅读(319)  评论(0编辑  收藏  举报