0

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 ?

Codi
  • 511
  • 3
  • 19
  • ```StringBuilder``` or ```StringBuffer```? – Mati Aug 11 '15 at 09:24
  • You have to use you're `String` as a stream: http://stackoverflow.com/questions/247161/how-do-i-turn-a-string-into-a-stream-in-java – Asoub Aug 11 '15 at 09:25

0 Answers0