CharArrayReader 用於寫入數據符,它繼承於Writer。操作的數據是以字符為單位!
CharArrayWriter 函數列表
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)
Writer和CharArrayWriter源碼分析
Writer是CharArrayWriter的父類,我們先看看Writer的源碼,然後再學CharArrayWriter的源碼。
1. Writer源碼分析(基於jdk1.7.40)
package java.io;
public abstract class Writer implements Appendable, Closeable, Flushable {
private char[] writeBuffer;
private final int writeBufferSize = 1024;
protected Object lock;
protected Writer() {
this.lock = this;
}
protected Writer(Object lock) {
if (lock == null) {
throw new NullPointerException();
}
this.lock = lock;
}
public void write(int c) throws IOException {
synchronized (lock) {
if (writeBuffer == null){
writeBuffer = new char[writeBufferSize];
}
writeBuffer[0] = (char) c;
write(writeBuffer, 0, 1);
}
}
public void write(char cbuf[]) throws IOException {
write(cbuf, 0, cbuf.length);
}
abstract public void write(char cbuf[], int off, int len) throws IOException;
public void write(String str) throws IOException {
write(str, 0, str.length());
}
public void write(String str, int off, int len) throws IOException {
synchronized (lock) {
char cbuf[];
if (len <= writeBufferSize) {
if (writeBuffer == null) {
writeBuffer = new char[writeBufferSize];
}
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);
}
}
public Writer append(CharSequence csq) throws IOException {
if (csq == null)
write("null");
else
write(csq.toString());
return this;
}
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;
}
public Writer append(char c) throws IOException {
write(c);
return this;
}
abstract public void flush() throws IOException;
abstract public void close() throws IOException;
}
2. CharArrayWriter 源碼分析(基於jdk1.7.40)
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是寫入的長度
// 查看本欄目
說明:
CharArrayWriter實際上是將數據寫入到“字符數組”中去。
(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中。
注意:該函數返回CharArrayWriter對象!
(08) append(CharSequence csq)的作用將csq寫入到CharArrayWriter中。
注意:該函數返回CharArrayWriter對象!
(09) writeTo(OutputStream out) 將該“字符數組輸出流”的數據全部寫入到“輸出流out”中。
示例代碼
關於CharArrayWriter中API的詳細用法,參考示例代碼(CharArrayWriterTest.java):
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