Posts Tagged ‘OutputStream’

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.