所有类


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。
 
方法摘要
 StringWriterappend(char c)
          将指定字符追加到此 writer。
 StringWriterappend(CharSequence csq)
          将指定的字符序列追加到此 writer。
 StringWriterappend(CharSequence csq, int start, int end)
          将指定字符序列的子序列追加到此 writer。
 voidclose()
          关闭 StringWriter 无效。
 voidflush()
          刷新该流的缓冲。
 StringBuffergetBuffer()
          返回该字符串缓冲区本身。
 StringtoString()
          以字符串的形式返回该缓冲区的当前值。
 voidwrite(char[] cbuf, int off, int len)
          写入字符数组的某一部分。
 voidwrite(int c)
          写入单个字符。
 voidwrite(String str)
          写入一个字符串。
 voidwrite(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 - 一个指定缓冲区初始大小的 int。
方法详细信息

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