A:关键字
protected char buf[]; 用于存放写入CharArrayWriter的字符、存满自动扩容。
protected int count; buf中现有的字符数
B:构造方法
public CharArrayWriter() 使用默认buf大小创建CharArrayWriter。
public CharArrayWriter(int initialSize) 使用指定的buf大小创建CharArrayWriter。
C:一般方法
CharArrayWriter append(CharSequence csq) 将一串有序字符序列写入buf中
CharArrayWriter append(CharSequence csq, int start, int end) 将一串有序字符序列的一部分写入buf中
CharArrayWriter append(char c) 将一个字符写入buf中
void close() 关闭此流(没有效果)
void flush() flush此流(没有效果)
void reset() 清空buf、重头开始
int size() 查看当前buf中字符总数
char[] toCharArray() 将buf中内容转换成char[]
String toString() 将buf中字符转换成String返回
void write(int c) 写入一个字符。
void write(char c[], int off, int len) 将一个char[]的一部分写入buf中、若buf满、扩容。
void write(String str, int off, int len) 将一个字符串写入buf中、满自动扩容
void writeTo(Writer out) 将buf中现有的字节写入到subWriter(out)中
3、源码分析
package com.chy.io.original.code;
import java.io.IOException;
import java.util.Arrays;
/**
* Writer的一个子类、可将字符写入到自带的一个缓存字符数组buf中、
* 当buf写满时、会自动扩容。
*/
public class CharArrayWriter extends Writer {
/**
* 用于存放写入CharArrayWriter的字符、存满自动扩容。
*/
protected char buf[];
/**
* buf中现有的字符数
*/
protected int count;
/**
* 使用默认buf大小创建CharArrayWriter。
*/
public CharArrayWriter() {
this(32);
}
/**
* 使用指定的buf大小创建CharArrayWriter。
*/
public CharArrayWriter(int initialSize) {
if (initialSize < 0) {
throw new IllegalArgumentException("Negative initial size: "
+ initialSize);
}
buf = new char[initialSize];
}
/**
* 写入一个字符。
*/
public void write(int c) {
synchronized (lock) {
int newcount = count + 1;
//如果buf存满、则将buf容量扩大1倍、并将原来buf中count字符copy到新的buf中
if (newcount > buf.length) {
buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
}
//将新写入的字符存入到buf第count个下标位置。
buf[count] = (char)c;
count = newcount;
}
}
/**
* 将一个char[]的一部分写入buf中、若buf满、扩容。
*/
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;
}
}
/**
* 将一个字符串写入buf中、满自动扩容
*/
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;
}
}
/**
* 将buf中现有的字节写入到subWriter(out)中
*/
public void writeTo(Writer out) throws IOException {
synchronized (lock) {
out.write(buf, 0, count);
}
}
/**
* 将一串有序字符序列写入buf中
*/
public CharArrayWriter append(CharSequence csq) {
String s = (csq == null "null" : csq.toString());
write(s, 0, s.length());
return this;
}
/**
* 将一串有序字符序列的一部分写入buf中
*/
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;
}
/**
* 将一个字符写入buf中
*/
public CharArrayWriter append(char c) {
write(c);
return this;
}
/**
* 清空buf、重头开始
*/
public void reset() {
count = 0;
}
/**
* 将buf中内容转换成char[]
*/
public char toCharArray()[] {
synchronized (lock) {
return Arrays.copyOf(buf, count);
}
}
/**
* 查看当前buf中字符总数
*/
public int size() {
return count;
}
/**
* 将buf中字符转换成String返回
*/
public String toString() {
synchronized (lock) {
return new String(buf, 0, count);
}
}
/**
* flush CharArrayWriter、因此方法对CharArrayWriter没有效果、所以方法体是空!
*/
public void flush() { }
/**
* 同样、关闭CharArrayWriter没有用、调用close()关闭此流、此流的方法一样能用。
*/
public void close() { }
}
4、实例演示:
package com.chy.io.original.test;
import java.io.BufferedReader;
import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author andyChen
* @version 1.1 , 13/11/15
*
*/
public class CharArrayTest {
private static final String str = "abcdefghijklmnopqrstuvwxyz";
private static char[] charArray = new char[26];
static{
for(int i=0; i
总结:
本质是将字符写入内置字符缓存数组中、或者是将字符从内置缓存数组读取到程序中(内置字符缓存数组中字符的来源是在构造CharArrayReader时传入的字符数组)。
更多IO内容:java_io 体系之目录