Tuesday 21 May 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...