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中文网其他相关文章!