Java CharArrayWriter类

CharArrayReader 用于写入数据符,它继承于Writer,实现了以数组作为目标的输出流。

构造函数

CharArrayWriter()
CharArrayWriter(int initialSize)

方法

CharArrayWriter     append(CharSequence csq, int start, int end)
CharArrayWriter     append(char c)
CharArrayWriter     append(CharSequence csq)
void     close()
void     flush()
void     reset()
int     size()
char[]     toCharArray()
String     toString()
void     write(char[] buffer, int offset, int len)
void     write(int oneChar)
void     write(String str, int offset, int count)
void     writeTo(Writer out)

源码解析

public class CharArrayWriter extends Writer {
    // 字符数组缓冲
    protected char buf[];
    // 下一个字符的写入位置
    protected int count;
    // 构造函数:默认缓冲区大小是32
    public CharArrayWriter() {
        this(32);
    }
    // 构造函数:指定缓冲区大小是initialSize
    public CharArrayWriter(int initialSize) {
        if (initialSize < 0) {
            throw new IllegalArgumentException("Negative initial size: "
                                               + initialSize);
        }
        buf = new char[initialSize];
    }

    // 写入一个字符c到CharArrayWriter中
    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;
        }
    }

    // 写入字符数组c到CharArrayWriter中。off是“字符数组b中的起始写入位置”,len是写入的长度
    public void write(char c[], int off, int len) {
        if ((off < 0) || (off > c.length) || (len < 0) ||
            ((off + len) > c.length) || ((off + len) < 0)) {
            throw new IndexOutOfBoundsException();
        } else if (len == 0) {
            return;
        }
        synchronized (lock) {
            int newcount = count + len;
            if (newcount > buf.length) {
                buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
            }
            System.arraycopy(c, off, buf, count, len);
            count = newcount;
        }
    }

    // 写入字符串str到CharArrayWriter中。off是“字符串的起始写入位置”,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;
        }
    }

    // 将CharArrayWriter写入到“Writer对象out”中
    public void writeTo(Writer out) throws IOException {
        synchronized (lock) {
            out.write(buf, 0, count);
        }
    }

    // 将csq写入到CharArrayWriter中
    // 注意:该函数返回CharArrayWriter对象
    public CharArrayWriter append(CharSequence csq) {
        String s = (csq == null ? "null" : csq.toString());
        write(s, 0, s.length());
        return this;
    }

    // 将csq从start开始(包括)到end结束(不包括)的数据,写入到CharArrayWriter中。
    // 注意:该函数返回CharArrayWriter对象! 
    public CharArrayWriter append(CharSequence csq, int start, int end) {
        String s = (csq == null ? "null" : csq).subSequence(start, end).toString();
        write(s, 0, s.length());
        return this;
    }

    // 将字符c追加到CharArrayWriter中!
    // 注意:它与write(int c)的区别。append(char c)会返回CharArrayWriter对象。
    public CharArrayWriter append(char c) {
        write(c);
        return this;
    }

    // 重置
    public void reset() {
        count = 0;
    }

    // 将CharArrayWriter的全部数据对应的char[]返回
    public char toCharArray()[] {
        synchronized (lock) {
            return Arrays.copyOf(buf, count);
        }
    }

    // 返回CharArrayWriter的大小
    public int size() {
        return count;
    }

    public String toString() {
        synchronized (lock) {
            return new String(buf, 0, count);
        }
    }

    public void flush() { }

    public void close() { }
}

例子

public static void main(String[] args) {
    String s = "这是java串";
    char[] dst = new char[s.length()]; //定义字符串
    s.getChars(0, s.length(), dst, 0); //将字符串转变为字符数组
    CharArrayReader charArrayReader = new CharArrayReader(dst); //输入流实例
    CharArrayWriter charArrayWriter = new CharArrayWriter(); //输出流实例
    try {
        while (charArrayReader.ready()) {
            charArrayWriter.write(charArrayReader.read()); //将输入流的内容写入输出流
        }
    } catch(IOException e) {
        e.printStackTrace();
    }
    //将输出流转换为字符数组
    char[] temp_char = charArrayWriter.toCharArray();
    System.out.println("字符数组的输出————————");
    //将输出流转换为字符串
    String temp_str = charArrayWriter.toString();
    for (int i = 0; i < temp_char.length; i++) {
        System.out.print(temp_char[i]);
    }
    System.out.println();
    System.out.println("字符串的输出——————————");
    System.out.println(temp_str);
}

用法总结

(01) 通过CharArrayWriter()创建的CharArrayWriter对应的字符数组大小是32。
(02) 通过CharArrayWriter(int size) 创建的CharArrayWriter对应的字符数组大小是size。
(03) write(int oneChar)的作用将int类型的oneChar换成char类型,然后写入到CharArrayWriter中。
(04) write(char[] buffer, int offset, int len) 是将字符数组buffer写入到输出流中,offset是从buffer中读取数据的起始偏移位置,len是读取的长度。
(05) write(String str, int offset, int count) 是将字符串str写入到输出流中,offset是从str中读取数据的起始位置,count是读取的长度。
(06) append(char c)的作用将char类型的c写入到CharArrayWriter中,然后返回CharArrayWriter对象。
注意:append(char c)与write(int c)都是将单个字符写入到CharArrayWriter中。它们的区别是,append(char c)会返回CharArrayWriter对象,但是write(int c)返回void。
(07) append(CharSequence csq, int start, int end)的作用将csq从start开始(包括)到end结束(不包括)的数据,写入到CharArrayWriter中。
(08) append(CharSequence csq)的作用将csq写入到CharArrayWriter中。
(09) writeTo(OutputStream out) 将该“字符数组输出流”的数据全部写入到“输出流out”中。



版权声明:本文为JAVASCHOOL原创文章,未经本站允许不得转载。