原文: https://beginnersbook.com/2018/05/java-9-try-with-resources-enhancements/

try-with-resource语句首先在 Java 7 中引入。该语句在 Java 9 中得到了重大改进。在本指南中,我们将讨论 Java 9 中try-with-resource语句的改进

什么是try-with-resource

这个语句最初是在 Java 7 中引入的,以避免我们为异常处理编写的冗余代码。这句话的优点是:

  1. 尝试用资源自动关闭所有资源(文件,数据库连接,网络连接等)。无需明确关闭它们。这可以防止内存泄漏。
  2. 借助try-with-resource,我们可以减少不必要的代码行,使代码更具可读性。

我们以前如何使用try-with-resource在 Java 7 中编写代码?

这就是我们在 Java 7 中使用Try-With-Resource语句的方式。

  1. import java.io.FileNotFoundException;
  2. import java.io.FileOutputStream;
  3. public class JavaExample {
  4. public static void main(String[] args) throws FileNotFoundException {
  5. try(FileOutputStream fileOutputStream = new FileOutputStream("beginnersbook.txt");){
  6. //We are writing this string in the output file using FileOutputStream
  7. String mystring = "We are writing this line in the output file.";
  8. //Converting the given string in bytes
  9. byte bytes[] = mystring.getBytes();
  10. //Writing the bytes into the file
  11. fileOutputStream.write(bytes);
  12. //Displaying success message after the successful write operation
  13. System.out.println("The given String is written in the file successfully");
  14. }catch(Exception e) {
  15. System.out.println(e);
  16. }
  17. }
  18. }

输出:

  1. The given String is written in the file successfully

Java 7 中的try-with-resource问题

Java 7 中的Try-With-Resource语句存在某些问题。此语句不允许在语句块(范围)之外声明资源。让我们举一个例子来理解这一点。

Java 7 - 在 Try-With-Resources 块之外声明的资源

  1. import java.io.FileNotFoundException;
  2. import java.io.FileOutputStream;
  3. public class JavaExample {
  4. public static void main(String[] args) throws FileNotFoundException {
  5. FileOutputStream fileOutputStream = new FileOutputStream("beginnersbook.txt");
  6. try(fileOutputStream){
  7. String mystring = "We are writing this line in the output file.";
  8. byte bytes[] = mystring.getBytes();
  9. fileOutputStream.write(bytes);
  10. System.out.println("The given String is written in the file successfully");
  11. }catch(Exception e) {
  12. System.out.println(e);
  13. }
  14. }
  15. }

Java 7 中的输出:

  1. Compile-time error

上面的示例抛出编译时错误,因为资源是在Try-With-Resource语句的范围之外声明的。

Java 7 - 外部声明的资源 - 重复资源作为变通方法

为了解决上述错误,我们不得不在 Java 7 中做一个解决方法。我们过去常常复制资源引用,如下所示:

  1. import java.io.FileNotFoundException;
  2. import java.io.FileOutputStream;
  3. public class JavaExample {
  4. public static void main(String[] args) throws FileNotFoundException {
  5. FileOutputStream fileOutputStream = new FileOutputStream("beginnersbook.txt");
  6. try(FileOutputStream fileOutputStream2 = fileOutputStream){
  7. String mystring = "We are writing this line in the output file.";
  8. byte bytes[] = mystring.getBytes();
  9. fileOutputStream2.write(bytes);
  10. System.out.println("The given String is written in the file successfully");
  11. }catch(Exception e) {
  12. System.out.println(e);
  13. }
  14. }
  15. }

这段代码在 Java 7 中运行良好。
注意try块中的FileOutputStream fileOutputStream2 = fileOutputStream行。我们在Try-With-Resource的范围内创建了对已声明的输出流的另一个引用。

Java 9 - try-with-resource改进

Java 9 为传统的Try-With-Resource语句提供了一个重要的改进。 Java 9 允许我们在 Try-With-Resource之外声明资源。我们不再需要创建局部变量来访问资源。让我们采用与 Java 7 相同的示例,但遇到了编译错误。在 Java 9 中,此代码运行得非常好。

  1. import java.io.FileNotFoundException;
  2. import java.io.FileOutputStream;
  3. public class JavaExample {
  4. public static void main(String[] args) throws FileNotFoundException {
  5. FileOutputStream fileOutputStream = new FileOutputStream("beginnersbook.txt");
  6. try(fileOutputStream){
  7. String mystring = "We are writing this line in the output file.";
  8. byte bytes[] = mystring.getBytes();
  9. fileOutputStream.write(bytes);
  10. System.out.println("The given String is written in the file successfully");
  11. }catch(Exception e) {
  12. System.out.println(e);
  13. }
  14. }
  15. }

输出:

  1. The given String is written in the file successfully

Eclipse Oxygen 运行 Java SE 9 中此代码的屏幕截图。

Java 9 - `try-with-resource`改进 - 图1