【问题标题】:Close file in finally block doesn't work在 finally 块中关闭文件不起作用
【发布时间】:2012-02-17 09:21:52
【问题描述】:
try {
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);
    String line = null;
} catch (FileNotFoundException fnf) {
    fnf.printStackTrace();
} finally {
    fr.close();
}

fr.close() 显示错误:

fr 无法解决

我读过在 finally 块中关闭文件是一个好习惯。
这到底是怎么回事?

【问题讨论】:

    标签: java try-catch-finally


    【解决方案1】:

    变量fr 仅在try 块内具有作用域。它超出了 finally 块的范围。您需要在try之前声明它:

    FileReader fr = null;
    try {
        fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String line = null;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (fr != null) {
            try {
                fr.close();
            } catch (IOException e) {
                // This is unrecoverable. Just report it and move on
                e.printStackTrace();
            }
        }
    }
    

    这是一种相当常见的代码模式,因此最好记住它以备将来类似情况使用。

    考虑从这个方法中抛出 IOException - 打印跟踪轨迹对调用者不是很有帮助,并且你不需要在 fr.close() 周围嵌套 try catch

    【讨论】:

    • 如果我这样做了,那么它说我需要在关闭文件时添加另一个 Try 块。我读过之前的一篇文章,上面说对此无能为力。那么,这将是最好的方式吗?
    • 是的,这很烦人。人们经常编写一种名为“closeQuietly”(或类似名称)的实用方法来执行此操作。它捕获并忽略异常。
    • 查看嵌套try/catch 的编辑答案 - 也是一种常见模式
    • 有理由不在外面做FileReader fr = new FileReader(file);吗?这将消除检查fr != null 的需要,不是吗?一个复杂的问题是catch (FileNotFoundException e) 必须移到外部尝试:gist.github.com/cben/6e092536e8f2072ab83f 但这对我来说仍然更清晰。但也许这只是我的Python habits
    【解决方案2】:

    现在不需要 finally 块了,

    try (FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);){
    
        String line = null;
    
        }
    
    } catch(FileNotFoundException fnf) {
        fnf.printStackTrace();
    } 
    

    现在自动关闭你的阅读器

    【讨论】:

    • 是的,但仅限于 Java 7。Java 6 及更早版本没有此功能。
    • 如果有人想知道这是为什么,请搜索“try with resources”
    • 更多信息:try-with-resources 语句确保每个资源在语句结束时关闭。
    【解决方案3】:

    您的示波器有问题。如果您真的想使用该语法,您应该像这样修复它:

    FileReader fr = null;
    try {
        fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String line = null;
    } catch (FileNotFoundException fnf) {
        fnf.printStackTrace();
    } finally {
        if( fr != null)
           fr.close();
    }
    

    这样,fr 将存在于 finally 的块范围内。

    【讨论】:

      【解决方案4】:
      public static void main(String[] args) throws IOException{
              FileReader file1 = null;
              try{
                  file1 = new FileReader("blaaa.txt");//this file does not exist
              }
              catch (FileNotFoundException e){}
              catch (IOException e)  {e.printStackTrace();}
              finally {
                  try{file1.close();}
                  catch (NullPointerException e){
                  }
                  finally {
                      System.out.println("Thank you, please try again");
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-15
        • 2013-08-17
        • 1970-01-01
        • 2021-12-15
        • 2012-02-16
        • 2016-04-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多