最佳答案
如果抛出异常,我希望关闭缓冲的读取器和文件读取器,并释放资源。
public static Object[] fromFile(String filePath) throws FileNotFoundException, IOException
{
try (BufferedReader br = new BufferedReader(new FileReader(filePath)))
{
return read(br);
}
}
但是,是否需要有一个 catch
子句才能成功关闭?
编辑:
基本上,以上 Java7中的代码是否等同于以下 Java6中的代码:
public static Object[] fromFile(String filePath) throws FileNotFoundException, IOException
{
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader(filePath));
return read(br);
}
catch (Exception ex)
{
throw ex;
}
finally
{
try
{
if (br != null) br.close();
}
catch(Exception ex)
{
}
}
return null;
}