StringWriter class

 

                  See Writer class for descriptions of StringWriter's inherited methods.  They are:

 

.close( )                                    Closes the stream, flushing it first.

.flush( )                                    Flushes all associated buffers in a stream without closing it.

.write( int c )                             Writes a single char, supplied as the low-order two bytes of a four-byte int.

.write( char[ ] c )                       Writes an array of characters.

.write( char[ ] c, int off, int len )  Writes a portion len of a character array, beginning at offset

.write( String s )                        Writes a String.

.write( String s, int off, int len )  Writes a portion len of a String, beginning at off

 

plus:

 

.getBuffer( )     below

.toString( )

 

 

  Creating “dynamic” Strings.  A StringWriter can be used to accumulate characters into a dynamic internal buffer for later manipulation as a familiar String or StringBuffer.  This can effectively give you a "dynamic" String to play with, which is not immutable. 

 

 You can add things to your "dynamic String" using StringWriter's own write( ) methods (above), as the examples of toString( ) and getBuffer( ) below do. Or you can use a PrintWriter, as this next snippet does.  i.e. This code prints the following line:

 

                                    Look at these PrintWriter print results: 55 5.555 0.5 true

 

import java.io.*;

  

StringWriter myDynamicString = new StringWriter( );

PrintWriter pw = new PrintWriter( myDynamicString );

// the above statement could be: PrintWriter pw = new PrintWriter( new StringWriter( ); );

pw.print( "Look at these PrintWriter print( ) results: " );

pw.print( 55 + " " );

pw.print( 5.555 + " " );

pw.print( 0.5 + " " );

pw.print( (5 == 5) + " ");

pw.println( );

String s = myDynamicString.toString( );

System.out.print( s );

 

  No StringWriter constructors or methods except close( ) throw IOExceptions, although accompanying chained streams may throw them.

 

 

  StringWriter adds two methods to those provided by Writer. They are getBuffer( ) and toString( ).  Both are explained below.

 

 

String  .toString( )  method

 

  Returns the contents of the StringWriter as a familiar String. i.e.

 

import java.io.*;

 

StringWriter sw = new StringWriter( );              // create a new StringWriter

sw.write( "Hello World" );                    // add "Hello World"  to it

String s = sw.toString( );                     // get it back in a String

System.out.println( s );                                       // print  the String

 

 

StringBuffer  .getBuffer( ) method

 

  Returns the contents of the StringWriter as a familiar StringBuffer. i.e.

 

import java.io.*;

 

StringBuffer sb;

StringWriter sw = new StringWriter( );              // create a new StringWriter

sw.write( "Hello " );                                               // add "Hello" to it

sb  = sw.getBuffer( );                                           // get it back in a StringBuffer

sb.append( "World");                                          // append "World"

System.out.println( sb );                     // print the StringBuffer

 

  Combining both approaches from above we have:

 

import java.io.*;

 

StringWriter sw = new StringWriter( );

sw.write( "Hello " );

StringBuffer sb = new StringBuffer( sw.toString( ) );

sb.append( "World");

System.out.println( sb );