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
1)处理顺序:
一般情况下是:先打开的后关闭,后打开的先关闭
另一种情况:看依赖关系,如果流a依赖流b,应该先关闭流a,再关闭流b
例如处理流a依赖节点流b,应该先关闭处理流a,再关闭节点流b
当然完全可以只关闭处理流,不用关闭节点流。处理流关闭的时候,会调用其处理的节点流的关闭方法
如果将节点流关闭以后再关闭处理流,会抛出IO异常
2)处理方法:
在finally块中关闭I/O流,每个流关闭时分别用try/catch处理异常
finally {
try {
if (null != reader) {
reader.close();
}
} catch (IOException e){
throw new RuntimeException("关闭失败");
}
try {
...
}