Writer
public abstract class Writer implements Appendable, Closeable, Flushable
用于写入字符流的抽象类
Writer append(char c) 将指定的字符附加到此作者。
Writer append(CharSequence csq) 将指定的字符序列附加到此作者。
Writer append(CharSequence csq, int start, int end) 将指定字符序列的子序列附加到此作者。
abstract void close() 关闭流,先刷新。
abstract void flush() 刷新流。
void write(char[] cbuf) 写入一个字符数组。
abstract void write(char[] cbuf, int off, int len) 写入字符数组的一部分。
void write(int c) 写一个字符
void write(String str) 写一个字符串
void write(String str, int off, int len) 写一个字符串的一部分。
void write(int c)
public void write(int c) throws IOException {
synchronized (lock) {
if (writeBuffer == null){
writeBuffer = new char[WRITE_BUFFER_SIZE];
}
writeBuffer[0] = (char) c;
write(writeBuffer, 0, 1);
}
}
abstract public void write(char cbuf[], int off, int len) throws IOException;
void write(String str, int off, int len)
public void write(String str, int off, int len) throws IOException {
synchronized (lock) {
char cbuf[];
if (len <= WRITE_BUFFER_SIZE) {
if (writeBuffer == null) {
writeBuffer = new char[WRITE_BUFFER_SIZE];
}
cbuf = writeBuffer;
} else { // Don't permanently allocate very large buffers.
cbuf = new char[len];
}
str.getChars(off, (off + len), cbuf, 0);
write(cbuf, 0, len);
}
}
Writer append(CharSequence csq)
public Writer append(CharSequence csq, int start, int end) throws IOException {
CharSequence cs = (csq == null ? "null" : csq);
write(cs.subSequence(start, end).toString());
return this;
}
OutputStreamWriter
public class OutputStreamWriter extends Writer
OutputStreamWriter是字符的桥梁流以字节流:向其写入的字符编码成使用指定的字节charset。
void write(String str, int off, int len)
private final StreamEncoder se; // 真正处理类
public void write(String str, int off, int len) throws IOException {
se.write(str, off, len);
}
FileWriter
public class FileWriter extends OutputStreamWriter
public FileWriter(String fileName) throws IOException {
super(new FileOutputStream(fileName));
}
没有实现类 全都使用的是父类 OutputStreamWriter的方法
BufferedWriter
public class BufferedWriter extends Writer
OutputStreamWriter是字符的桥梁流以字节流:向其写入的字符编码成使用指定的字节charset
void write(int c)
public void write(int c) throws IOException {
synchronized (lock) {
ensureOpen();
if (nextChar >= nChars) //
flushBuffer();
cb[nextChar++] = (char) c;
}
}
void flushBuffer() 把缓存的字符写出
void flushBuffer() throws IOException {
synchronized (lock) {
ensureOpen();
if (nextChar == 0)
return;
out.write(cb, 0, nextChar);
nextChar = 0;
}
}
void write(char cbuf[], int off, int len)
public void write(char cbuf[], int off, int len) throws IOException {
synchronized (lock) {
.... // 数据判断
if (len >= nChars) { // 要写入的字符长度大于储存的上限
flushBuffer(); //写入现有的输入
out.write(cbuf, off, len); //写入这次的数据
return;
}
int b = off, t = off + len;
while (b < t) {
int d = min(nChars - nextChar, t - b); //比较缓存
System.arraycopy(cbuf, b, cb, nextChar, d); //数据拷贝到缓存
b += d;
nextChar += d;
if (nextChar >= nChars) //缓存数据满了
flushBuffer(); //写入
}
}
}
void write(String s, int off, int len)
public void write(String s, int off, int len) throws IOException {
synchronized (lock) {
ensureOpen();
int b = off, t = off + len;
while (b < t) {
int d = min(nChars - nextChar, t - b);
s.getChars(b, b + d, cb, nextChar);
b += d;
nextChar += d;
if (nextChar >= nChars)
flushBuffer();
}
}
}
CharArrayWriter
class CharArrayWriter extends Writer
该类实现了可以用作Writer的字符缓冲区。 当数据写入流时,缓冲区会自动增长
public void write(int c) {
synchronized (lock) {
int newcount = count + 1;
if (newcount > buf.length) {
buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
}
buf[count] = (char)c;
count = newcount;
}
}
void write(String str, int off, int len)
public void write(String str, int off, int len) {
synchronized (lock) {
int newcount = count + len;
if (newcount > buf.length) {
buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
}
str.getChars(off, off + len, buf, count);
count = newcount;
}
}
char toCharArray()[]
public char toCharArray()[] {
synchronized (lock) {
return Arrays.copyOf(buf, count);
}
}