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

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

一、问题

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

二、范例,使用try-finally

使用try-finally来关闭资源,如下所示:

 public class FirstLineOfFile_Version1 {
static String firstLineOfFile(String path) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
br.close();
}
}
}

如果有两个资源,我们会这样来写,但是不推荐这样做。

 public class Copy_Version1 {
final static int BUFFER_SIZE = 1024;

static void copy(String src, String dst) throws IOException {
InputStream in = new FileInputStream(src);
try {
OutputStream out = new FileOutputStream(dst);
try {
byte[] buf = new byte[BUFFER_SIZE];
int n;
while ((n = in.read(buf)) > 0) {
out.write(buf, 0, n);
}
} finally {
out.close();
}
} finally {
in.close();
}
}
}

这样写都能正确关闭资源,但是不推荐这样写,为什么呢?

因为在try块和finally块中都会抛出异常。在这种情况下第二个异常会完全抹除第一个异常。在异常堆栈轨迹中就看不到第一个异常的记录。在现实系统中调试会变得异常复杂。

三、范例,使用try-with-resources

Java 7引入了try-with-resources语句,解决了上述问题。要使用这个构造的资源,就必须实现AutoClosable接口。如果编写了一个类,如果它代表了是必须被关闭的资源,那么这个类也应该实现AutoClosable接口。下面来重写firstLineFoFile以及copy方法:

 public class FirstLineOfFile_Version2 {
static String firstLineOfFile(String path) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
return br.readLine();
}
}
}

如果调用readLine和close方法抛异常,会抛出第一个异常,第二个异常会被禁止。这些禁止的异常不是被抛弃了也会打印在异常堆栈中。

 public class Copy_Version2 {
final static int BUFFER_SIZE = 1024;

static void copy(String src, String dst) throws IOException {
try (InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst)
) {
byte[] buf = new byte[BUFFER_SIZE];
int n;
while ((n = in.read(buf)) > 0) {
out.write(buf, 0, n);
}

}
}
}

try-with-resources还可以使用catch子句,这样即可以处理异常,又不需要再套用一层代码。

 public class FirstLineOfFile_Version3 {
static String firstLineOfFile(String path, String defaultVal) {
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
return br.readLine();
} catch (IOException e) {
return defaultVal;
}
}
}

四、总结

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

标签: Java

添加新评论