How to send data from OutputStream to a String in Java?

The solution is relatively simple but it got me a while until I’ve found a solution on the net.
So:
You have to use a ByteArrayOutputStream, a constructor of String and know the character encoding used by the method which you get your OutputStream from.
A little code:
ByteArrayOutputStream os = new ByteArrayOutputStream();
aClass.outputStreamMethod(os);
String aString = new String(os.toByteArray(),"UTF-8");
And that’s all:) Pay attention to the character encoding.

Be the first to leave a comment. Don’t be shy.

Join the Discussion

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>