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中文網其他相關文章!