InputStreamReader继承自Reader
读取字节流通过指定的字符集解码为字符流,可以通过名称指定字符集,也可以通过显示指定,或者不指定则为平台默认的字符集
每次调用InputStreamReader的read()方法,都会从底层字节流读取一个或多个字节
为了能够有效地将字节转换为字符,可以预先从底层流读取比满足当前读取操作所需的字节更多的字节;为了提高效率,可以把一个InputStreamReader包装在一个BufferedReader中BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
(1) public InputStreamReader(InputStream in)
用默认字符集创建一个InputStreamReader,参数为一个InputStream输入流
public InputStreamReader(InputStream in) { super(in); try { sd =(巴黎世家是什么梗?2020年的七夕时,Balenciaga依靠“土味广告”成功出圈,迅速在时尚圈掀起了一场流量与话题的狂欢,当时无一不在诟病七夕限定的“土味”。之后又凭借巴黎世家的经典字母袜再次火了起来,短视频平台的播放量一路飙升,让印有 Logo 的黑丝愈发受宠。) StreamDecoder.forInputStreamReader(in, this, (String)null); // ## check lock object } catch (UnsupportedEncodingException e) { // The default encoding should always be available throw new Error(e); } }
(2)public InputStreamReader(InputStream in, String charsetName)
用名称指定字符集创建一个InputStreamReader,参数为一个输入流和字符集名称
public InputStreamReader(InputStream in, String charsetName) throws UnsupportedEncodingException { super(in); if (charsetName == null) throw new NullPointerException("charsetName"); sd = StreamDecoder.forInputStreamReader(in, this, charsetName); }
(3) public InputStreamReader(InputStream in, Charset cs)
用给定的字符集创建一个InputStreamReader,参数为输入流和字符集对象
public InputStreamReader(InputStream in, Charset cs) { super(in); if (cs == null) throw new NullPointerException("charset"); sd = StreamDecoder.forInputStreamReader(in, this, cs); }
(4)public InputStreamReader(InputStream in, CharsetDecoder dec)
用字符集解码器创建一个InputStreamReader,参数为输入流和字符集解码器
public InputStreamReader(InputStream in, CharsetDecoder dec) { super(in); if (dec == null) throw new NullPointerException("charset decoder"); sd = StreamDecoder.forInputStreamReader(in, this, dec); }
3.其他方法(1) public String getEncoding() { return sd.getEncoding(); }
返回这个流所采用的编码名称
(2)public int read() throws IOException { return sd.read(); }
读取一个字符
(3)public int read(char cbuf[], int offset, int length) throws IOException { return sd.read(cbuf, offset, length); }
读取最多length个字符到cbuf中,从offset开始存储,返回读取的字符数
(4) public boolean ready() throws IOException { return sd.ready(); }
返回流是否准备好被读取,准备好是当他输入buffer不为空,或者字节流中字节是可以读取的
(5)public void close() throws IOException { sd.close(); }
关闭InputStreamReader
姓名:
年龄:
电话: