Is there a clean and simple way to convert an instance of java.io.PrintWriter
into a java.io.PrintStream
?
Asked
Active
Viewed 8,317 times
1 Answers
10
First obtain an OutputStream
from the Writer
. See this question
Then pass it as argument to the PrintStream
constructor:
OutputStream os = new WriterOutputStream(writer);
PrintStream ps = new PrintStream(os);
Update: commons-io 2.0 has WriterOutputStream
, so use it.
-
1Unfortunately the code linked in that question is copyright, and therefore not suitable for my needs. I'm happy to accept that I need to implement this myself. – Armand Nov 24 '10 at 15:34
-
that should not be a problem. It is "Licensed under the Apache License, Version 2.0". I'd bet you are using a number of thus-licensed libraries already – Bozho Nov 24 '10 at 15:35