Is there a simple and safe way to convert a PrintWriter into a PrintStream?

前端 未结 1 1329
青春惊慌失措
青春惊慌失措 2021-01-04 23:42

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

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-05 00:30

    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.

    0 讨论(0)
提交回复
热议问题