API说明:在字符串缓冲区中收集输出的字符流,可用于构造字符串, 关闭流无效,关闭后调用其他方法不会报异常
//字符串缓冲过区
private StringBuffer buf;
/**
* 利用默认大小的StringBuffer创建字符输出流
*/
public StringWriter() {
buf = new StringBuffer();
lock = buf;
}
/**
* 使用指定大小的StringBuffer创建字符输出流
*/
public StringWriter(int initialSize) {
if (initialSize < 0) {
throw new IllegalArgumentException("Negative buffer size");
}
buf = new StringBuffer(initialSize);
lock = buf;
}
1)write(int c):写入一个字符到字符串缓冲区中
write(int c)
2)write(char cbuf[], int off, int len):从指定字符数组的下标off开始写len个字节到流中
/**
* 从指定字符数组的下标off开始写len个字节到流中
*/
public void write(char cbuf[], int off, int len) {
if ((off < 0) || (off > cbuf.length) || (len < 0) ||
((off + len) > cbuf.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return;
}
buf.append(cbuf, off, len);
}
3)write(String str):写一个字符串到流中
/**
* 写一个字符串到流中
*/
public void write(String str) {
buf.append(str);
}
4)write(String str, int off, int len):写一个字符串的一部分到流中
/**
* 写一个字符串的一部分到流中
*/
public void write(String str, int off, int len) {
buf.append(str.substring(off, off + len));
}
5)检索字符输出流的数据
/**
* 将缓冲对象的值转成字符串输出
*/
public String toString() {
return buf.toString();
}
/**
* 返回流的字符缓冲对象StringBuff
*
* @return StringBuffer holding the current buffer value.
*/
public StringBuffer getBuffer() {
return buf;
}
6)close():关闭流无效,关闭后调用该类方法不会报异常
/**
* 关闭流无效,关闭后调用该类方法不会报异常
*/
public void close() throws IOException {
}
暂未使用过,故不清楚什么时候用,用在什么地方,所以先了解功能