首页  >  文章  >  Java  >  StringWriter在Java中的重要性是什么?

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

PHPz
PHPz转载
2023-09-10 16:57:031295浏览

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中的重要性是什么?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除