相关疑难解决方法(0)

java尝试finally块来关闭流

我想在finally块中关闭我的流,但它会抛出一个IOException所以我似乎必须tryfinally块中嵌套另一个块才能关闭流.这是正确的方法吗?看起来有点笨重.

这是代码:

 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)

java file-io finally try-catch stream

31
推荐指数
5
解决办法
3万
查看次数

标签 统计

file-io ×1

finally ×1

java ×1

stream ×1

try-catch ×1