How to check whether an OutputStream is closed

后端 未结 7 1226
南笙
南笙 2020-12-10 10:29

Is there anyway to check whether an OutputStream is closed without attempting to write to it and catching the IOException?

For example, consider the fol

7条回答
  •  不思量自难忘°
    2020-12-10 11:10

    Unfortunately OutputStream API does not have method like isClosed().

    So, I know only one clear way: create your class StatusKnowingOutputStream that wraps any other output stream and implements its close() method as following:

    public void close() {
        out.close();
        closed = true;
    }
    

    Now add method isClosed()

    public boolean isClosed() {
        return closed;
    }
    

提交回复
热议问题