关于java:Effective-Java第9条trywithresources优先于tryfinally

1次阅读

共计 1880 个字符,预计需要花费 5 分钟才能阅读完成。

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。这样写的代码简洁、清晰,产生的异样也更有参考价值。

正文完
 0