字符流
Reader
public abstract class Reader extends Object implements Readable, Closeable
用于读取字符流的抽象类。
实现的接口
Closeable , AutoCloseable , Readable
类图

方法
| 变量和类型 | 方法 | 描述 |
|---|---|---|
abstract void |
close() |
关闭流并释放与其关联的所有系统资源。
|
void |
mark(int readAheadLimit) |
标记流中的当前位置。
|
boolean |
markSupported() |
判断此流是否支持mark()操作。
|
static Reader |
nullReader() |
返回不读取任何字符的新
Reader 。 |
int |
read() |
读一个字符。
|
int |
read(char[] cbuf) |
将字符读入数组。
|
abstract int |
read(char[] cbuf, int off, int len) |
将字符读入数组的一部分。
|
int |
read(CharBuffer target) |
尝试将字符读入指定的字符缓冲区。
|
boolean |
ready() |
判断此流是否可以读取。
|
void |
reset() |
重置流。
|
long |
skip(long n) |
跳过字符。
|
long |
transferTo(Writer out) |
读取此阅读器中的所有字符,并按照读取的顺序将字符写入给定的编写器。
|
Writer
public abstract class Writer extends Object implements Appendable, Closeable, Flushable 用于写入字符流的抽象类
实现的接口
Closeable , Flushable , Appendable , AutoCloseable
类图

方法
| 变量和类型 | 方法 | 描述 |
|---|---|---|
void |
flush() |
刷新流。
|
String |
getEncoding() |
返回此流使用的字符编码的名称。
|
void |
write(char[] cbuf, int off, int len) |
写一个字符数组的一部分。
|
void |
write(int c) |
写一个字符。
|
void |
write(String str, int off, int len) |
写一个字符串的一部分。
|
示例
FileWriter writer = new FileWriter("char.txt"); writer.write("髣髴兮若轻云之蔽月,飘飖兮若流风之回雪"); // 不调用刷新方法或者关闭方法,数据在缓冲区中,不会写入文件 writer.flush(); // writer.close(); FileReader reader = new FileReader("char.txt"); char[] chars = new char[100]; System.out.println(new String(chars, 0, reader.read(chars)));
posted on
浙公网安备 33010602011771号