天天看點

java中io各種流的關閉順序

還是先看API

void close()

          Closes this stream and releases any system resources associated with it.

close

void close() throws IOException

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

Throws:

IOException - if an I/O error occurs

關閉該流并釋放與之關聯的所有資源。在關閉該流後,再調用 read()、ready()、mark()、reset() 或 skip() 将抛出 IOException。關閉以前關閉的流無效。

[html]

public void close() throws IOException { 

    synchronized (lock) { 

        if (in == null) 

        return; 

        in.close(); 

        in = null; 

        cb = null; 

    } 

一般情況下是:先打開的後關閉,後打開的先關閉

另一種情況:看依賴關系,如果流a依賴流b,應該先關閉流a,再關閉流b

例如處理流a依賴節點流b,應該先關閉處理流a,再關閉節點流b

當然完全可以隻關閉處理流,不用關閉節點流。處理流關閉的時候,會調用其處理的節點流的關閉方法

如果将節點流關閉以後再關閉處理流,會抛出IO異常