Io汇总(五) Writer 以及子类

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);
        }
    }

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值