简体   繁体   English

当我尝试在finally块中关闭BufferedReader时,为什么eclipse会抱怨?

[英]Why is eclipse complaining when I try to close BufferedReader in finally block?

Here is my code: 这是我的代码:

public static String readFile()
    {

        BufferedReader br = null;
        String line;
        String dump="";

        try
        {
            br = new BufferedReader(new FileReader("dbDumpTest.txt"));
        }
        catch (FileNotFoundException fnfex)
        {
            System.out.println(fnfex.getMessage());
            System.exit(0);
        }

        try
        {
            while( (line = br.readLine()) != null)
            {
                dump += line + "\r\n";
            }
        }
        catch (IOException e)
        {
            System.out.println(e.getMessage() + " Error reading file");
        }
        finally
        {
            br.close();
        }
        return dump;

So eclipse is complaining about an unhandled IO exception caused by br.close(); 因此,eclipse抱怨br.close();导致未处理的IO异常br.close();

Why would this cause an IO exception? 为什么这会导致IO异常?

My second question is why eclipse doesn't complain about the following code: 我的第二个问题是为什么eclipse不会抱怨以下代码:

InputStream is = null; 
      InputStreamReader isr = null;
      BufferedReader br = null;

      try{
         // open input stream test.txt for reading purpose.
         is = new FileInputStream("c:/test.txt");

         // create new input stream reader
         isr = new InputStreamReader(is);

         // create new buffered reader
         br = new BufferedReader(isr);

         // releases any system resources associated with reader
         br.close();

         // creates error
         br.read();

      }catch(IOException e){

         // IO error
         System.out.println("The buffered reader is closed");
      }finally{

         // releases any system resources associated
         if(is!=null)
            is.close();
         if(isr!=null)
            isr.close();
         if(br!=null)
            br.close();
      }
   }
}

I'd appreciate it if you kept the explanation in Laymen's terms if possible. 如果可以的话,请以Laymen的名义保留解释,我们将不胜感激。 Thanks for the help in advance 我在这里先向您的帮助表示感谢

Both code examples should have compiler errors complaining about an unhandled IOException. 这两个代码示例都应具有抱怨未处理的IOException的编译器错误。 Eclipse shows these as errors in both code examples for me. Eclipse在我的两个代码示例中将它们显示为错误。

The reason is that the close method throws an IOException , a checked exception, when called in the finally block, which is outside a try block. 原因是close方法在try块之外的finally块中调用时, close方法将抛出IOException ,这是一个经过检查的异常。

The fix is to use a try-with-resources statement , which is available in Java 1.7+. 解决方法是使用try-with-resources语句 ,该语句在Java 1.7+中可用。 The resources declared are implicitly closed. 声明的资源被隐式关闭。

try (BufferedReader br = new BufferedReader(new FileReader("dbDumpTest.txt")))
{
   // Your br processing code here
}
catch (IOException e)
{
   // Your handling code here
}
// no finally necessary.

Prior to Java 1.7, you need to wrap the calls to close() in their own try-catch blocks inside the finally block. 在Java 1.7之前,您需要将对close()的调用包装在finally块内自己的try-catch块中。 It's a lot of verbose code to ensure that everything is closed and cleaned up. 有很多冗长的代码可以确保所有内容都已关闭并清理。

finally
{
    try{ if (is != null) is.close(); } catch (IOException ignored) {}
    try{ if (isr != null) isr.close(); } catch (IOException ignored) {}
    try{ if (br != null) br.close(); } catch (IOException ignored) {}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 try-with-resources 或在 sonarqube 的“finally”子句中关闭此“BufferedReader” - Use try-with-resources or close this "BufferedReader" in a "finally" clause in sonarqube 使用 try-with-resources 或在“finally”子句中关闭此“BufferedReader” - Use try-with-resources or close this "BufferedReader" in a "finally" clause java尝试finally块来关闭流 - java try finally block to close stream Java序列化:在try或finally块中关闭流? - Java Serialization: close streams in try or in a finally block? 我什么时候应该在Java的try-catch-finally中使用finally-block - When should I use the finally-block in Java's try-catch-finally 我需要用 try/catch/finally 块包围 fileInputStream.close 吗? 它是如何完成的? - do I need to surround fileInputStream.close with a try/catch/finally block? How is it done? 为什么 JDBC 连接需要在 finally 块中关闭? - Why JDBC connections needs to close in finally block? 在try \\ finally块中是否存在这样的情况,最终将不会被执行? - Is there such case when in try\finally block the finally won't be executed? SonarQube 不断抱怨 Use try-with-resources 或在“finally”子句中关闭此“PreparedStatement” - SonarQube keeps complaining about Use try-with-resources or close this "PreparedStatement" in a "finally" clause eclipse要求我在finally块中使用try / catch包围 - 可以禁用它吗? - eclipse asks me to surround with try/catch in finally block - possible to disable it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM