The previous byte input and output streams can be adapted to be emptied arbitrarily, but in actual use, we mostly use bytes.
First, the inheritance relationship diagram: The
picture is taken from: http://www.cnblogs.com/skywang12345/p/io_01.html
input stream Reader
The top-level object of the character input stream is the Reader (abstract class). There are the following methods:
method | return value | illustrate | Remark |
---|---|---|---|
read(char cbuf[], int off, int len) | int | Basic core reading | core, abstract, abstract method |
read(java.nio.CharBuffer target) | int | read | |
read() | int | read | |
read(char cbuf[]) | int | read | |
skip(long n) | long | skip character | |
ready() | boolean | Determine if it can be read | |
markSupported() | boolean | Determine whether tags are supported | |
mark(int readAheadLimit) | void | mark | Use mark and reset together |
reset() | void | reset | |
close() | void | close stream |
in addition:
- Contains the internal members of protected Object lock; for locking and ensuring synchronization.
output stream Writer
method | return value | illustrate | Remark |
---|---|---|---|
write(char cbuf[], int off, int len) | void | core, abstract, abstract method | |
write(int c) | void | write | --- |
write(char cbuf[]) | void | write | --- |
write(String str) | void | write | --- |
write(String str, int off, int len) | void | write | --- |
append(char c) | Writer | continuous writing | 支持append(XXX).append(XXX) |
append(CharSequence csq) | Writer | continuous writing | The parameter can be String, support append(XXX).append(XXX) |
append(CharSequence csq, int start, int end) | Writer | continuous writing | The parameter can be String, support append(XXX).append(XXX) |
flush() | void | situation cache | --- |
close() | void | closure | --- |
in addition:
- 1. Internal members including protected Object lock; are used for locking to ensure synchronization.
- Contains the internal members of private char[] writeBuffer; with its own buffer.
- CharSequence is the interface of String, so append(CharSequence csq) can
input/output stream implementation object
ByteArrayInputStream 和 ByteArrayOutputStream
Input and output streams for character arrays:
name | Superior inheritance relationship | illustrate | core | Remark |
---|---|---|---|---|
CharArrayReader | Reader | input stream of char array | Essence: protected char buf[] |
|
CharArrayWriter | Writer | output stream of char array | Essence: protected char buf[] |
Note:
It is a character array itself, which is actually very similar to ByteArrayInputStream and ByteArrayOutputStream.
PipedReader和PipedWriter
Pipeline communication input and output streams:
name | Superior inheritance relationship | illustrate | core | Remark |
---|---|---|---|---|
PipedReader | Reader | input stream for pipe communication | Essence: char buffer[] |
Use with PipedWriter |
PipedWriter | Writer | output stream for pipe communication | 本质: private PipedReader sink; |
Use with PipedReader |
InputStreamReader和OutputStreamWriter
Character input and output streams:
name | Superior inheritance relationship | illustrate | core | Remark |
---|---|---|---|---|
InputStreamReader | Reader | character input stream | Essence: private final StreamDecoder sd; |
Created in the way of new InputStreamReader(InputStream in), converted from byte stream, it is an extension |
OutputStreamWriter | Writer | character output stream | Essence: private final StreamEncoder se; |
new OutputStreamWriter(OutputStream out)的方式创建,从字节流转换,算是一种扩展 |
注:
想进行字符串的话,更加的扩展是PrintWriter,new PrintWriter(new FileOutputStream(file))的方式使用。
FileReader和FileWriter
文件字符输入输出流:
名称 | 上级继承关系 | 说明 | 核心 | 备注 |
---|---|---|---|---|
FileReader | InputStreamReader | 文件字符输入流 | 本质: 完全是调用的InputStreamReader |
|
FileWriter | OutputStreamWriter | 文件字符输出流 | 本质: 完全是调用的FileOutputStream |
BufferedReader和BufferedWriter
缓冲字符输入输出流:
名称 | 上级继承关系 | 说明 | 核心 | 备注 |
---|---|---|---|---|
BufferedReader | Reader | 缓冲字符输入流 | 本质: private char cb[] |
有readLine(),可以很方便的读取一行 |
BufferedWriter | Writer | 缓冲字符输出流 | 本质: private char cb[] |
注意,write写入后,不会马上反应,调用flush()清空缓存后反应 |