I have a program in which I use a PrintStream
object to write to a file. Is there a way to use something like a String Buffer or similar as the OutputStream
for the object instead, so that I can use print
to append to the String ?
I later want to be able to retrieve this String (obviously)
(ps. i wanna do this for testing purposes in my program)
Some info:
PrintStream ps = new PrintStream(myOutputStream);
...
ps.print(...);
...
ps.close();
I want to replace myOutputStream
with some kind of String buffer which I can retrieve later and read.
As far as I know, in C++ we can do this as
stringstream ss;
ss << ...
How to do this in Java ?