Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output stream over a StringBuilder [duplicate]

Tags:

java

stream

I think this has been answered but I can't seem to find it.

I have an instance method which writes some contents to an output stream

writeTo(OutputStream){
     //class specific logic
}

I want it to get these contents into a StringBuilder. I can do this via a temporary file but that does not seem right. I want to do something like:

Stringbuilder sb = /* */;
OutputStream os = outForStringBuilder(sb);//not sure how to do this
instance.writeTo(os); //This should write the contents to Stringbuilder
like image 658
rocketboy Avatar asked Aug 24 '26 09:08

rocketboy


1 Answers

Use a ByteArrayOutputStream and then call toString(charSet) - no need for a StringBuilder.

like image 182
Tom Avatar answered Aug 25 '26 23:08

Tom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!