- java.lang.Object
-
- java.io.Writer
-
- java.io.StringWriter
-
- 实现的所有接口
-
Closeable,Flushable,Appendable,AutoCloseable
public class StringWriter extends Writer
在字符串缓冲区中收集其输出的字符流,然后可用于构造字符串。关闭
StringWriter无效。 在关闭流之后可以调用此类中的方法,而不生成IOException。- 从以下版本开始:
- 1.1
-
-
构造方法摘要
构造方法 构造器 描述 StringWriter()使用默认的初始字符串缓冲区大小创建新的字符串编写器。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
nullWriter, write
-
-
-
-
构造方法详细信息
-
StringWriter
public StringWriter()
使用默认的初始字符串缓冲区大小创建新的字符串编写器。
-
StringWriter
public StringWriter(int initialSize)
使用指定的初始字符串缓冲区大小创建新的字符串writer。- 参数
-
initialSize- 在自动扩展之前适合此缓冲区的char值的数量 - 异常
-
IllegalArgumentException- 如果initialSize为负数
-
-
方法详细信息
-
write
public void write(char[] cbuf, int off, int len)写一个字符数组的一部分。- Specified by:
-
write类Writer - 参数
-
cbuf- 字符数组 -
off- 开始写入字符的偏移量 -
len- 要写入的字符数 - 异常
-
IndexOutOfBoundsException- 如果off为负数,或者len为负数,或者off + len为负数或大于给定数组的长度
-
write
public void write(String str, int off, int len)
写一个字符串的一部分。- 重写:
-
write类Writer - 参数
-
str- 要写入的字符串 -
off- 开始写入字符的偏移量 -
len- 要写入的字符数 - 异常
-
IndexOutOfBoundsException- 如果off为负数,或者len为负数,或者off + len为负数或大于给定字符串的长度
-
append
public StringWriter append(CharSequence csq)
将指定的字符序列追加到此writer。调用
out.append(csq)形式的此方法的行为与调用的方式完全相同out.write(csq.toString())取决于规范
toString字符序列csq,整个序列可以不追加。 例如,调用字符缓冲区的toString方法将返回一个子序列,其内容取决于缓冲区的位置和限制。- Specified by:
-
append,界面Appendable - 重写:
-
append类Writer - 参数
-
csq- 要追加的字符序列。 如果csq是null,则将四个字符"null"附加到该写入器。 - 结果
- 这位作家
- 从以下版本开始:
- 1.5
-
append
public StringWriter append(CharSequence csq, int start, int end)
将指定字符序列的子序列追加到此writer。形式的这种方法的调用
out.append(csq, start, end)时csq不是null,行为以完全相同的方式调用out.write(csq.subSequence(start, end).toString())- Specified by:
-
append,界面Appendable - 重写:
-
append类Writer - 参数
-
csq- 将附加子序列的字符序列。 如果csq是null,则将附加字符,就像csq包含四个字符"null"。 -
start- 子序列中第一个字符的索引 -
end- 子序列中最后一个字符后面的字符的索引 - 结果
- 这位作家
- 异常
-
IndexOutOfBoundsException- 如果start或end为负数,则start大于end,或者end大于csq.length() - 从以下版本开始:
- 1.5
-
append
public StringWriter append(char c)
将指定的字符追加到此writer。调用
out.append(c)形式的此方法的行为与调用的方式完全相同out.write(c)- Specified by:
-
append,界面Appendable - 重写:
-
append,类Writer - 参数
-
c- 要追加的16位字符 - 结果
- 这位作家
- 从以下版本开始:
- 1.5
-
getBuffer
public StringBuffer getBuffer()
返回字符串缓冲区本身。- 结果
- StringBuffer保存当前缓冲区值。
-
close
public void close() throws IOException关闭StringWriter无效。 在关闭流之后可以调用此类中的方法,而不生成IOException。- Specified by:
-
close,界面AutoCloseable - Specified by:
-
close,界面Closeable - Specified by:
-
close类Writer - 异常
-
IOException- 如果发生I / O错误
-
-