一、JAVA的IO流使用了裝飾模式,關閉最外面的流的時候會自動調用被包裝的流的close()方法?
二、若是按順序關閉流,是從內層流到外層流關閉仍是從外層到內存關閉?java
BufferedInputStream.close()方法this
public void close() throws IOException { byte[] buffer; while ( (buffer = buf) != null) { if (bufUpdater.compareAndSet(this, buffer, null)) { InputStream input = in; in = null; if (input != null) input.close(); return; } // Else retry in case a new buf was CASed in fill() } }
能夠上述代碼能夠看出,BufferedInputStream.close()方法調用了input.close()方法。code
JAVA的IO流使用了裝飾模式,關閉最外面的流的時候會自動調用被包裝的流的close()方法。內存