上一篇博客讲了IO流中的字节流,分别介绍输入字节流和输出字节流的类结构图; 主要介绍字节流的读取方式和输出方式,分表对应着read()方法和write()方法;然后介绍不同形式的来源有不同的读取方式,同时根据输出的目的地不同有不同的输出方式。具体的内容可以看这里,这里我们介绍IO流中的字符流。
1 输入字符流
输入字符流的父类是抽象类Reader,它除了实现Closeable接口,还需要实现Readable接口;
Abstract class for reading character streams. The only methods that a subclass must implement are read(char[], int, int) and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both.
用于读取字符流的抽象类。子类必须实现的惟一方法是read(char[],int,int)和close()。但是,大多数子类将覆盖这里定义的一些方法,以便提供更高的效率、额外的功能。
public abstract class Reader implements Readable, Closeable {
protected Object lock;
protected Reader() {
this.lock = this;
}
protected Reader(Object lock) {
if (lock == null) {
throw new NullPointerException();
}
this.lock = lock;
}
public int read(java.nio.CharBuffer target) throws IOException {
int len = target.remaining();
char[] cbuf = new char[len];
int n = read(cbuf, 0, len);
if (n > 0)
target.put(cbuf, 0, n);
return n;
}
public int read() throws IOException {
char cb[] = new char[1];
if (read(cb, 0, 1) == -1)
return -1;
else
return cb[0];
}
public int read(char cbuf[]) throws IOException {
return read(cbuf, 0, cbuf.length);
}
abstract public int