如果任何读取器或流装饰另一个读取器/流,则关闭外部读取器/流也会关闭内部.这可以从Javadoc暗示Closeable#close()
:
关闭此流并释放与其关联的所有系统资源.
这也适用于底层资源.
如果你非常好奇,你可以深入研究这些课程的来源,例如.在BufferedReader
:
public void close() throws IOException { synchronized (lock) { if (in == null) return; try { in.close(); } finally { in = null; cb = null; } } }
in
底层在哪里Reader
.
如果任何读取器或流装饰另一个读取器/流,则关闭外部读取器/流也会关闭内部.这可以从Javadoc暗示Closeable#close()
:
关闭此流并释放与其关联的所有系统资源.
这也适用于底层资源.
如果你非常好奇,你可以深入研究这些课程的来源,例如.在BufferedReader
:
public void close() throws IOException { synchronized (lock) { if (in == null) return; try { in.close(); } finally { in = null; cb = null; } } }
in
底层在哪里Reader
.
是的,装饰的溪流也是封闭的.
InputStream in = new FileInputStream("c:\\myfile.txt"); InputStreamReader reader = new InputStreamReader(in); BufferedReader bufferedReader = new BufferedReader(reader); bufferedReader.close(); in.read(); // throws an IOException (no such file or directory) reader.read(); // throws an IOException (Stream closed)