java.lang.Object
java.io.Writer
java.io.StringWriter
- 所有已实现的接口:
Closeable,Flushable,Appendable,AutoCloseable
将其输出收集到字符串缓冲区中的字符流,然后可用于构造字符串。
关闭 StringWriter 没有任何效果。可以在流关闭后调用此类中的方法而不生成 IOException 。
- 自从:
- 1.1
-
字段摘要
-
构造方法总结
构造方法 -
方法总结
修饰符和类型方法描述append(char c) 将指定的字符附加到此编写器。append(CharSequence csq) 将指定的字符序列附加到此编写器。append(CharSequence csq, int start, int end) 将指定字符序列的子序列附加到此 writer。voidclose()关闭StringWriter没有任何效果。voidflush()冲洗流。返回字符串缓冲区本身。toString()将缓冲区的当前值作为字符串返回。voidwrite(char[] cbuf, int off, int len) 写入字符数组的一部分。voidwrite(int c) 写一个字符。void写一个字符串。void写入字符串的一部分。在类 java.io.Writer 中声明的方法
nullWriter, write
-
构造方法详细信息
-
StringWriter
public StringWriter()使用默认的初始字符串缓冲区大小创建一个新的字符串编写器。 -
StringWriter
public StringWriter(int initialSize) 使用指定的初始字符串缓冲区大小创建一个新的字符串编写器。- 参数:
initialSize- 在自动扩展之前适合此缓冲区的char值的数量- 抛出:
IllegalArgumentException- 如果initialSize为负
-
-
方法详情
-
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"会附加到这个 writer。- 返回:
- 这位作家
- 自从:
- 1.5
-
append
将指定字符序列的子序列附加到此 writer。当
csq不是null时调用out.append(csq, start, end)形式的方法,其行为与调用完全相同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 错误
-