A
character stream that collects its output in a string buffer, which can then be
used to construct a string.
import java.io.StringWriter; public class StringWriterEx { public static void main(String args[]){ StringWriter writer = new StringWriter(); writer.write("first line "); System.out.println(writer); writer.write("Second line"); System.out.println(writer); } }
Output
first line first line Second line
StringWriter
internally use StringBuffer, to store data. You can get the buffer using
“getBuffer” method of StringWriter. Whatever the changes you do to the buffer is
reflected to writer.
import java.io.StringWriter; public class StringWriterEx { public static void main(String args[]){ StringWriter writer = new StringWriter(); StringBuffer buffer = writer.getBuffer(); buffer.append("first line"); System.out.println(writer); buffer.append("Second line"); System.out.println(writer); } }
Output
first line first lineSecond line
No comments:
Post a Comment