thinking in java (二十七) ----- IO之CharArrayWriter(字元陣列輸出流)
CharArrayWriter介紹
用於寫入字元資料,繼承於writer,操作的是單位為字元。
原始碼分析
CharArrayWriter
package java.io; import java.util.Arrays; 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() { } }
CharArrayWriter實際上是將資料寫入“字元陣列”中
總結:
1,通過CharArrayWriter()建立的CharArrayWriter對應的字元陣列大小是32
2,通過CharArrayWriter(int size)建立的CharArrayWriter對應字元陣列大小是size
3,writer(int oneChar)的作用是將int型別的oneChar換成char型別,然後寫入到CharArrayWriter中
4,write(char[] buffer, int offset ,intcount)是將字元陣列buffer寫入到輸出流中,offset是從buffer中讀取資料的起始便宜位置,len是讀取的長度,
5,write(String str, int offset ,intcount)作用是將字串寫入輸出流中,offset是讀取資料的起始位置,count是讀取的長度
6,append(char c)的作用是將char型別的c寫入到CharArrayWriter中,他們的區別是append(char c)會返回一個CharArrayWriter物件,但是write(int c )返回void
7,append(CharSequence csq, int start, int end)的作用將csq從start開始(包括)到end結束(不包括)的資料,寫入到CharArrayWriter中。
8,append(CharSequence csq)的作用將csq寫入到CharArrayWriter中。
9,writeTo(OutputStream out) 將該“字元陣列輸出流”的資料全部寫入到“輸出流out”中。
示例程式碼
package io;
import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.IOException;
/**
* CharArrayWriter 測試程式
*
* @author skywang
*/
public class CharArrayWriterTest {
private static final int LEN = 5;
// 對應英文字母“abcdefghijklmnopqrstuvwxyz”
private static final char[] ArrayLetters = new char[] {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
public static void main(String[] args) {
tesCharArrayWriter() ;
}
/**
* CharArrayWriter的API測試函式
*/
private static void tesCharArrayWriter() {
try {
// 建立CharArrayWriter字元流
CharArrayWriter caw = new CharArrayWriter();
// 寫入“A”個字元
caw.write('A');
// 寫入字串“BC”個字元
caw.write("BC");
//System.out.printf("caw=%s\n", caw);
// 將ArrayLetters陣列中從“3”開始的後5個字元(defgh)寫入到caw中。
caw.write(ArrayLetters, 3, 5);
//System.out.printf("caw=%s\n", caw);
// (01) 寫入字元0
// (02) 然後接著寫入“123456789”
// (03) 再接著寫入ArrayLetters中第8-12個字元(ijkl)
caw.append('0').append("123456789").append(String.valueOf(ArrayLetters), 8, 12);
System.out.printf("caw=%s\n", caw);
// 計算長度
int size = caw.size();
System.out.printf("size=%s\n", size);
// 轉換成byte[]陣列
char[] buf = caw.toCharArray();
System.out.printf("buf=%s\n", String.valueOf(buf));
// 將caw寫入到另一個輸出流中
CharArrayWriter caw2 = new CharArrayWriter();
caw.writeTo(caw2);
System.out.printf("caw2=%s\n", caw2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
執行結果:
caw=ABCdefgh0123456789ijkl
size=22
buf=ABCdefgh0123456789ijkl
caw2=ABCdefgh0123456789ijkl
原文:http://www.cnblogs.com/skywang12345/p/io_19.html