System.out closed? Can I reopen it?

前端 未结 7 2403
逝去的感伤
逝去的感伤 2020-12-06 09:34

I was helping a friend to write some Java code, who doesn\'t know a lot about Java. So I wrote him some helper functions to easily get things done that are a little quirky i

7条回答
  •  一向
    一向 (楼主)
    2020-12-06 10:25

    Let's look at this from the caller's point of view.

    The caller has an OutputStream of some sort, and calls a method called write(). After the call completes, the caller discovers that the stream has been closed.

    In my view, your write() method simply should not call printer.close(). The latter closes the stream provided by the caller and is probably not what the caller expects.

    If you need to flush the stream, you could use flush().

提交回复
热议问题