Posts Tagged ‘Streams’

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.
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();
String aString = new String(os.toByteArray(),"UTF-8");
And that’s all:) Pay attention to the character encoding.