9

Is there a clean and simple way to convert an instance of java.io.PrintWriter into a java.io.PrintStream?

Armand
  • 23,463
  • 20
  • 90
  • 119

1 Answers1

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.

Community
  • 1
  • 1
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 1
    Unfortunately 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