Java Reference
In-Depth Information
public
StringWriter(int size)
Creates a new
StringWriter
with the specified initial buffer
size. Providing a good initial size estimate for the buffer will
improve performance in many cases.
public StringBuffer
getBuffer()
Returns the actual
StringBuffer
being used by this stream.
Because the actual
StringBuffer
is returned, you should take
care not to modify it while it is being used as an output des-
tination.
public String
toString()
Returns the current contents of the buffer as a
String
.
The following code uses a
StringWriter
to create a string that contains
the output of a series of
println
calls on the contents of an array:
public static String arrayToStr(Object[] objs) {
StringWriter strOut = new StringWriter();
PrintWriter out = new PrintWriter(strOut);
for (int i = 0; i < objs.length; i++)
out.println(i + ": " + objs[i]);
return strOut.toString();
}
20.5.8.
Print
Streams
The
Print
streams
PrintStream
and
PrintWriter
provide methods that make
it easy to write the values of primitive types and objects to a stream, in
a human-readable text formatas you have seen in many examples. The
Print
streams provide
print
and
println
methods for the following types: