StringWriter在Java中的重要性是什么?

StringWriter在Java中的重要性是什么?

StringWriter类是Writer类的子类,它将字符串写入输出流。为了写入字符串,该字符流将字符串收集到字符串缓冲区中,然后构造一个字符串。 StringWriter的缓冲区根据数据自动增长。 StringWriter类的重要方法有write()、append()、getBuffer()flush()close()。 p>

语法

<strong>public class StringWriter extends Writer</strong>

示例

import java.io.*;
public class StringWriterTest {
   public static void main(String args[]) {
      String str = "Welcome to Tutorials Point";
      try {
         StringWriter sw = new StringWriter();
         sw.write(str);
         StringBuffer sb = new StringBuffer();
         sb = sw.getBuffer();
         System.out.println("StringBuffer: "+ sb);
         System.out.println("String written by StringWriter: "+ sw);
         sw.close();
      } catch (IOException ioe) {
         System.out.println(ioe);
      }
   }
}

输出

StringBuffer: Welcome to Tutorials Point
String written by StringWriter: Welcome to Tutorials Point

以上就是StringWriter在Java中的重要性是什么?的详细内容,更多请关注其它相关文章!