模块 java.base
 java.io

类 StringWriter

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

public class StringWriter extends Writer
将其输出收集到字符串缓冲区中的字符流,然后可用于构造字符串。

关闭 StringWriter 没有任何效果。可以在流关闭后调用此类中的方法而不生成 IOException

自从:
1.1
  • 构造方法详细信息

    • StringWriter

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

      public StringWriter(int initialSize)
      使用指定的初始字符串缓冲区大小创建一个新的字符串编写器。
      参数:
      initialSize - 在自动扩展之前适合此缓冲区的 char 值的数量
      抛出:
      IllegalArgumentException - 如果 initialSize 为负
  • 方法详情

    • write

      public void write(int c)
      写一个字符。
      重写:
      write 在类 Writer
      参数:
      c - 指定要写入的字符的 int
    • write

      public void write(char[] cbuf, int off, int len)
      写入字符数组的一部分。
      指定者:
      write 在类 Writer
      参数:
      cbuf - 字符数组
      off - 开始写入字符的偏移量
      len - 要写入的字符数
      抛出:
      IndexOutOfBoundsException - 如果 off 为负数,或 len 为负数,或 off + len 为负数或大于给定数组的长度
    • write

      public void write(String  str)
      写一个字符串。
      重写:
      write 在类 Writer
      参数:
      str - 要写入的字符串
    • 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)
      将指定的字符序列附加到此编写器。

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

         out.write(csq.toString()) 

      根据字符序列 csqtoString 规范,可能不会附加整个序列。例如,调用字符缓冲区的 toString 方法将返回一个子序列,其内容取决于缓冲区的位置和限制。

      指定者:
      append 在接口 Appendable
      重写:
      append 在类 Writer
      参数:
      csq - 要追加的字符序列。如果 csqnull ,那么四个字符 "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())
        
      指定者:
      append 在接口 Appendable
      重写:
      append 在类 Writer
      参数:
      csq - 将追加子序列的字符序列。如果 csqnull ,那么字符将被追加,就好像 csq 包含四个字符 "null" 一样。
      start - 子序列中第一个字符的索引
      end - 子序列中最后一个字符之后的字符的索引
      返回:
      这位作家
      抛出:
      IndexOutOfBoundsException - 如果 startend 为负,则 start 大于 end,或者 end 大于 csq.length()
      自从:
      1.5
    • append

      public StringWriter  append(char c)
      将指定的字符附加到此编写器。

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

         out.write(c) 
      指定者:
      append 在接口 Appendable
      重写:
      append 在类 Writer
      参数:
      c - 要追加的 16 位字符
      返回:
      这位作家
      自从:
      1.5
    • toString

      public String  toString()
      将缓冲区的当前值作为字符串返回。
      重写:
      toString 在类 Object
      返回:
      对象的字符串表示形式。
    • getBuffer

      public StringBuffer  getBuffer()
      返回字符串缓冲区本身。
      返回:
      保存当前缓冲区值的 StringBuffer。
    • flush

      public void flush()
      冲洗流。

      StringWriterflush 方法什么都不做。

      指定者:
      flush 在接口 Flushable
      指定者:
      flush 在类 Writer
    • close

      public void close() throws IOException
      关闭 StringWriter 没有任何效果。可以在流关闭后调用此类中的方法而不生成 IOException
      指定者:
      close 在接口 AutoCloseable
      指定者:
      close 在接口 Closeable
      指定者:
      close 在类 Writer
      抛出:
      IOException - 如果发生 I/O 错误