java.lang.Object
java.io.Writer
java.io.StringWriter
- 所有已实现的接口:
-
Closeable,Flushable,Appendable,AutoCloseable
一个字符流,它将输出收集到一个字符串缓冲区中,然后可以用来构造一个字符串。
关闭StringWriter没有任何效果。在流关闭后仍然可以调用此类中的方法,而不会生成IOException。
- 自:
- 1.1
-
Field Summary
-
Constructor Summary
ConstructorsConstructorDescription使用默认的初始字符串缓冲区大小创建一个新的字符串写入器。StringWriter(int initialSize) 使用指定的初始字符串缓冲区大小创建一个新的字符串写入器。 -
Method Summary
Modifier and TypeMethodDescriptionappend(char c) 将指定的字符追加到此写入器。append(CharSequence csq) 将指定的字符序列追加到此写入器。append(CharSequence csq, int start, int end) 将指定字符序列的子序列追加到此写入器。voidclose()关闭StringWriter没有任何效果。voidflush()刷新流。返回字符串缓冲区本身。toString()将缓冲区的当前值作为字符串返回。voidwrite(char[] cbuf, int off, int len) 写入字符数组的一部分。voidwrite(int c) 写入单个字符。void写入字符串。void写入字符串的一部分。Methods declared in class java.io.Writer
nullWriter, write
-
Constructor Details
-
StringWriter
public StringWriter()使用默认的初始字符串缓冲区大小创建一个新的字符串写入器。 -
StringWriter
public StringWriter(int initialSize) 使用指定的初始字符串缓冲区大小创建一个新的字符串写入器。- 参数:
-
initialSize- 在自动扩展之前,此缓冲区可以容纳的char值的数量 - 抛出:
-
IllegalArgumentException- 如果initialSize为负数
-
-
Method Details
-
write
public void write(int c) 写入单个字符。 -
write
public void write(char[] cbuf, int off, int len) 写入字符数组的一部分。- 指定者:
-
write在类中Writer - 参数:
-
cbuf- 字符数组 -
off- 开始写入字符的偏移量 -
len- 要写入的字符数 - 抛出:
-
IndexOutOfBoundsException- 如果off为负数,或len为负数,或off + len为负数或大于给定数组的长度
-
write
写入字符串。 -
write
写入字符串的一部分。- 覆盖:
-
write在类中Writer - 参数:
-
str- 要写入的字符串 -
off- 开始写入字符的偏移量 -
len- 要写入的字符数 - 抛出:
-
IndexOutOfBoundsException- 如果off为负数,或len为负数,或off + len为负数或大于给定字符串的长度
-
append
将指定的字符序列追加到此写入器。out.append(csq)形式调用此方法的行为与调用方式完全相同out.write(csq.toString())根据字符序列
csq的toString规范,可能不会追加整个序列。例如,调用字符缓冲区的toString方法将返回一个内容取决于缓冲区的位置和限制的子序列。- 指定者:
-
append在接口中Appendable - 覆盖:
-
append在类中Writer - 参数:
-
csq- 要追加的字符序列。如果csq为null,则将四个字符"null"追加到此写入器。 - 返回:
- 此写入器
- 自:
- 1.5
-
append
将指定字符序列的子序列追加到此写入器。out.append(csq, start, end)形式调用此方法时,当csq不为null时,行为与调用方式完全相同out.write(csq.subSequence(start, end).toString())- 指定者:
-
append在接口中Appendable - 覆盖:
-
append在类中Writer - 参数:
-
csq- 从中追加子序列的字符序列。如果csq为null,则将字符追加为如果csq包含四个字符"null"。 -
start- 子序列中第一个字符的索引 -
end- 子序列中最后一个字符之后的索引 - 返回:
- 此写入器
- 抛出:
-
IndexOutOfBoundsException- 如果start或end为负数,start大于end,或end大于csq.length() - 自:
- 1.5
-
append
将指定的字符追加到此写入器。out.append(c)形式调用此方法的行为与调用方式完全相同out.write(c)- 指定者:
-
append在接口中Appendable - 覆盖:
-
append在类中Writer - 参数:
-
c- 要追加的16位字符 - 返回:
- 此写入器
- 自:
- 1.5
-
toString
返回缓冲区的当前值作为字符串。 -
getBuffer
返回字符串缓冲区本身。- 返回:
- 持有当前缓冲区值的StringBuffer。
-
flush
public void flush()刷新流。StringWriter的flush方法不执行任何操作。 -
close
关闭StringWriter没有任何效果。在流关闭后仍然可以调用此类中的方法,而不会生成IOException。- 指定者:
-
close在接口中AutoCloseable - 指定者:
-
close在接口中Closeable - 指定者:
-
close在类中Writer - 抛出:
-
IOException- 如果发生I/O错误
-