I have a java program where I am writing to a file using OutputStream. Everything is working fine. Upon completion of the program, I see data in my file. However, I would like to see this data on the System.out as well.
How can I convert OutputStream to a String so that I can simply System.out.println(); it? I tried fos.toString() but that prints java.io.FileOutputStream@1ed2e55e
Use a ByteArrayOutputStream and you can create a string from it. Assuming the 'stream' variable is a reference to your ByteArrayOutputStream in the following example, here's how you could do it...
System.out.println(new String(stream.toByteArray(), CharSet.defaultCharSet()));
EDIT: was missing a closing parenthesis above, fixed now
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With