Languages
[Edit]
EN

Java - convert PrintStream to String

6 points
Created by:
Zachariah
298

In this short article, we would like to show how to convert PrintStream to String in Java.

Quick solution:

 

Practical example

Edit

To convert PrintStream to String it is necessary to use some storage to collect PrintStream output data. As that storage ByteArrayOutputStream can be used that creates an in-memory array to keep data, returning them as String on toString() method call. UTF-8 charset allows using a universal encoding that is supported by almost all operating systems and programs.

Example output:

 

Alternative solutions

Edit

1. OutputStream with String constructor example

Edit

This approach converts ByteArrayOutputStream to byte array that is used to create String with constructor.

 Example output:

2. Custom OutputStream implementation example

Edit

This approach uses custom OutputStream implementation to convert written data to String.

 

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java conversion

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join