《Effective Java》第9条:try-with-resources优先于try-finally

打印 上一主题 下一主题

主题 908|帖子 908|积分 2724

220812_《Effective Java》第9条:try-with-resources优先于try-finally

一、问题

Java类库中包含许多需要通过调用close来关闭的资源,例如:InputStream、Output Stream和java.sql.Connection。在编程过程中如果没有关闭会产生性能问题。
二、范例,使用try-finally

使用try-finally来关闭资源,如下所示:
  1. public class FirstLineOfFile_Version1 {
  2.     static String firstLineOfFile(String path) throws IOException {
  3.         BufferedReader br = new BufferedReader(new FileReader(path));
  4.         try {
  5.             return br.readLine();
  6.         } finally {
  7.             br.close();
  8.         }
  9.     }
  10. }
复制代码
如果有两个资源,我们会这样来写,但是不推荐这样做。
  1. public class Copy_Version1 {
  2.     final static int BUFFER_SIZE = 1024;
  3.     static void copy(String src, String dst) throws IOException {
  4.         InputStream in = new FileInputStream(src);
  5.         try {
  6.             OutputStream out = new FileOutputStream(dst);
  7.             try {
  8.                 byte[] buf = new byte[BUFFER_SIZE];
  9.                 int n;
  10.                 while ((n = in.read(buf)) > 0) {
  11.                     out.write(buf, 0, n);
  12.                 }
  13.             } finally {
  14.                 out.close();
  15.             }
  16.         } finally {
  17.             in.close();
  18.         }
  19.     }
  20. }
复制代码
这样写都能正确关闭资源,但是不推荐这样写,为什么呢?
因为在try块和finally块中都会抛出异常。在这种情况下第二个异常会完全抹除第一个异常。在异常堆栈轨迹中就看不到第一个异常的记录。在现实系统中调试会变得异常复杂。
三、范例,使用try-with-resources

Java 7引入了try-with-resources语句,解决了上述问题。要使用这个构造的资源,就必须实现AutoClosable接口。如果编写了一个类,如果它代表了是必须被关闭的资源,那么这个类也应该实现AutoClosable接口。下面来重写firstLineFoFile以及copy方法:
  1. public class FirstLineOfFile_Version2 {
  2.     static String firstLineOfFile(String path) throws IOException {
  3.         try (BufferedReader br = new BufferedReader(new FileReader(path))) {
  4.             return br.readLine();
  5.         }
  6.     }
  7. }
复制代码
如果调用readLine和close方法抛异常,会抛出第一个异常,第二个异常会被禁止。这些禁止的异常不是被抛弃了也会打印在异常堆栈中。
  1. public class Copy_Version2 {
  2.     final static int BUFFER_SIZE = 1024;
  3.     static void copy(String src, String dst) throws IOException {
  4.         try (InputStream in = new FileInputStream(src);
  5.              OutputStream out = new FileOutputStream(dst)
  6.         ) {
  7.             byte[] buf = new byte[BUFFER_SIZE];
  8.             int n;
  9.             while ((n = in.read(buf)) > 0) {
  10.                 out.write(buf, 0, n);
  11.             }
  12.         }
  13.     }
  14. }
复制代码
try-with-resources还可以使用catch子句,这样即可以处理异常,又不需要再套用一层代码。
  1. public class FirstLineOfFile_Version3 {
  2.     static String firstLineOfFile(String path, String defaultVal) {
  3.         try (BufferedReader br = new BufferedReader(new FileReader(path))) {
  4.             return br.readLine();
  5.         } catch (IOException e) {
  6.             return defaultVal;
  7.         }
  8.     }
  9. }
复制代码
四、总结

在处理必须关闭的资源时,优先考虑try-with-resources。这样写的代码简洁、清晰,产生的异常也更有参考价值。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

惊雷无声

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表