Tuesday, May 21, 2019

java - How to use OutputStream to write to a String

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 ?

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...