我想在finally块中关闭我的流,但它会抛出一个IOException所以我似乎必须try在finally块中嵌套另一个块才能关闭流.这是正确的方法吗?看起来有点笨重.
这是代码:
public void read() {
try {
r = new BufferedReader(new InputStreamReader(address.openStream()));
String inLine;
while ((inLine = r.readLine()) != null) {
System.out.println(inLine);
}
} catch (IOException readException) {
readException.printStackTrace();
} finally {
try {
if (r!=null) r.close();
} catch (Exception e){
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)