JavaTM Platform
Standard Ed. 6

java.io
类 StringWriter

java.lang.Object
  继承者 java.io.Writer
      继承者 java.io.StringWriter
所有已实现的接口:
Closeable, Flushable, Appendable

public class StringWriter
extends Writer

一个字符流,可以用其回收在字符串缓冲区中的输出来构造字符串。

关闭 StringWriter 无效。此类中的方法在关闭该流后仍可被调用,而不会产生任何 IOException

从以下版本开始:
JDK1.1

字段摘要
 
从类 java.io.Writer 继承的字段
lock
 
构造方法摘要
StringWriter()
          使用默认初始字符串缓冲区大小创建一个新字符串 writer。
StringWriter(int initialSize)
          使用指定初始字符串缓冲区大小创建一个新字符串 writer。
 
方法摘要
 StringWriter append(char c)
          将指定字符添加到此 writer。
 StringWriter append(CharSequence csq)
          将指定的字符序列添加到此 writer。
 StringWriter append(CharSequence csq, int start, int end)
          将指定字符序列的子序列添加到此 writer。
 void close()
          关闭 StringWriter 无效。
 void flush()
          刷新该流的缓冲。
 StringBuffer getBuffer()
          返回该字符串缓冲区本身。
 String toString()
          以字符串的形式返回该缓冲区的当前值。
 void write(char[] cbuf, int off, int len)
          写入字符数组的某一部分。
 void write(int c)
          写入单个字符。
 void write(String str)
          写入一个字符串。
 void write(String str, int off, int len)
          写入字符串的某一部分。
 
从类 java.io.Writer 继承的方法
write
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

构造方法详细信息

StringWriter

public StringWriter()
使用默认初始字符串缓冲区大小创建一个新字符串 writer。


StringWriter

public StringWriter(int initialSize)
使用指定初始字符串缓冲区大小创建一个新字符串 writer。

参数:
initialSize - 在此缓冲区自动扩展前适合它的 char 值数。
抛出:
IllegalArgumentException - 如果 initialSize 为负
方法详细信息

write

public void write(int c)
写入单个字符。

覆盖:
Writer 中的 write
参数:
c - 指定要写入字符的 int。

write

public void write(char[] cbuf,
                  int off,
                  int len)
写入字符数组的某一部分。

指定者:
Writer 中的 write
参数:
cbuf - 字符数组
off - 开始写入字符处的偏移量
len - 要写入的字符数

write

public void write(String str)
写入一个字符串。

覆盖:
Writer 中的 write
参数:
str - 要写入的字符串

write

public void write(String str,
                  int off,
                  int len)
写入字符串的某一部分。

覆盖:
Writer 中的 write
参数:
str - 要写入的字符串
off - 开始写入字符处的偏移量
len - 要写入的字符数

append

public StringWriter append(CharSequence csq)
将指定的字符序列添加到此 writer。

out.append(csq) 的形式调用此方法,行为与以下调用完全相同:

     out.write(csq.toString()) 

可能不添加整个序列,也可能添加,具体取决于字符序列 csqtoString 指定。例如,调用一个字符缓冲区的 toString 方法将返回一个子序列,其内容取决于缓冲区的位置和限制。

指定者:
接口 Appendable 中的 append
覆盖:
Writer 中的 append
参数:
csq - 要添加的字符串序列。如果 csqnull,则向此 writer 添加四个字符 "null"
返回:
此 writer
从以下版本开始:
1.5

append

public StringWriter append(CharSequence csq,
                           int start,
                           int end)
将指定字符序列的子序列添加到此 writer。

csq 不为 null 时,调用该方法的 out.append(csq、 start、 end) 形式,行为与以下调用完全相同:

     out.write(csq.subSequence(start, end).toString()) 

指定者:
接口 Appendable 中的 append
覆盖:
Writer 中的 append
参数:
csq - 子序列将被添加的字符序列。如果 csqnull,则添加四个字符 "null",就好像 csq 包含它们一样。
start - 子序列中第一个字符的索引
end - 子序列中最后一个字符后面的字符的索引
返回:
此 writer
抛出:
IndexOutOfBoundsException - 如果 startend 为负,而 start 大于 end 或者 end 大于 csq.length()
从以下版本开始:
1.5

append

public StringWriter append(char c)
将指定字符添加到此 writer。

out.append(c) 的形式调用此方法,行为与以下调用完全相同:

     out.write(c) 

指定者:
接口 Appendable 中的 append
覆盖:
Writer 中的 append
参数:
c - 要添加的 16 位字符
返回:
此 writer
从以下版本开始:
1.5

toString

public String toString()
以字符串的形式返回该缓冲区的当前值。

覆盖:
Object 中的 toString
返回:
该对象的字符串表示形式。

getBuffer

public StringBuffer getBuffer()
返回该字符串缓冲区本身。

返回:
保存当前缓冲区值的 StringBuffer。

flush

public void flush()
刷新该流的缓冲。

指定者:
接口 Flushable 中的 flush
指定者:
Writer 中的 flush

close

public void close()
           throws IOException
关闭 StringWriter 无效。此类中的方法在关闭该流后仍可被调用,而不会产生任何 IOException

指定者:
接口 Closeable 中的 close
指定者:
Writer 中的 close
抛出:
IOException - 如果发生 I/O 错误

JavaTM Platform
Standard Ed. 6

提交错误或意见
有关更多的 API 参考资料和开发人员文档,请参阅 Java SE 开发人员文档。该文档包含更详细的、面向开发人员的描述,以及总体概述、术语定义、使用技巧和工作代码示例。

版权所有 2007 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策