简体   繁体   中英

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();

Why would this cause an IO exception?

My second question is why eclipse doesn't complain about the following code:

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. Thanks for the help in advance

Both code examples should have compiler errors complaining about an unhandled IOException. Eclipse shows these as errors in both code examples for me.

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.

The fix is to use a try-with-resources statement , which is available in 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. 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) {}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM