我想在 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();
}
}
}