当我尝试在 finally 块中关闭 BufferedReader 时,为什么 eclipse 会抱怨?
Why is eclipse complaining when I try to close BufferedReader in finally block?
这是我的代码:
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;
所以 eclipse 抱怨由 br.close();
引起的未处理的 IO 异常
为什么会导致IO异常?
我的第二个问题是为什么 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();
}
}
}
如果您能尽可能用通俗易懂的语言进行解释,我将不胜感激。预先感谢您的帮助
两个代码示例都应该有编译器错误,抱怨未处理的 IOException。 Eclipse 在我的两个代码示例中都将这些显示为错误。
原因是 close
方法在 try
块之外的 finally
块中调用时抛出一个 IOException
,一个检查异常。
修复方法是使用 try-with-resources statement,它在 Java 1.7+ 中可用。声明的资源隐式关闭。
try (BufferedReader br = new BufferedReader(new FileReader("dbDumpTest.txt")))
{
// Your br processing code here
}
catch (IOException e)
{
// Your handling code here
}
// no finally necessary.
在 Java 1.7 之前,您需要将对 close()
的调用包装在 finally
块内的它们自己的 try-catch 块中。这是很多冗长的代码,以确保关闭和清理所有内容。
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) {}
}
这是我的代码:
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;
所以 eclipse 抱怨由 br.close();
为什么会导致IO异常?
我的第二个问题是为什么 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();
}
}
}
如果您能尽可能用通俗易懂的语言进行解释,我将不胜感激。预先感谢您的帮助
两个代码示例都应该有编译器错误,抱怨未处理的 IOException。 Eclipse 在我的两个代码示例中都将这些显示为错误。
原因是 close
方法在 try
块之外的 finally
块中调用时抛出一个 IOException
,一个检查异常。
修复方法是使用 try-with-resources statement,它在 Java 1.7+ 中可用。声明的资源隐式关闭。
try (BufferedReader br = new BufferedReader(new FileReader("dbDumpTest.txt")))
{
// Your br processing code here
}
catch (IOException e)
{
// Your handling code here
}
// no finally necessary.
在 Java 1.7 之前,您需要将对 close()
的调用包装在 finally
块内的它们自己的 try-catch 块中。这是很多冗长的代码,以确保关闭和清理所有内容。
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) {}
}