Convert PrintStream to PrintWriter

Is there any possible way to convert PrintStream to PrintWriter (or vice versa) other than using WriterOutputStream , which is in apache common?

+7
source share
1 answer

To convert a PrintStream to a PrintWriter , use the constructor: PrintWriter(OutputStream out)

With this constructor, you run the risk of getting the wrong encoding, since PrintStream has an encoding, but using PrintWriter(OutputStream out) ignores this and just uses the default system encoding. If you do not need a default system, you will need to save the encoding in a separate field or variable and use:

 pw = new PrintWriter(new OutputStreamWriter(myPrintStream, encoding)); 

Where encoding can be (for example) "UTF-8" or an instance of Charset .

+10
source

All Articles