Yesterday I lamented that far too many Java APIs put useful data into a stream but fail to offer me an accessor method to obtain the data as a String. Today I know how to get that data as a String.
The secret is the ByteArrayOutputStream class. You "print" to this stream, then call toString() on it to get the String. Or you can call toByteArray() if you need that for some reason.
This looks like a painful extra step to my normal way of thinking, but I can see that a case might somehow be made that it is more flexible design, since the mechanism exists to take any method that outputs to streams and obtain the data in a String. I guess having methods that are inappropriately coupled to streams seems convenient when it's so inconvenient to access streams.
All I wanted to do yesterday was send the little chunk of XML I'd manipulated to log4j so I could see it in a test run and make sure it was right without having to write it to a file and go open the file to look at it. I think logging is a perfect example of why there ought to be accessors like this for every piece of data, rather than methods that force you to output.
Minimize side effects!