try-with-resources

try-with-resources 是 JDK 7 中一个新的异常处理机制,它能够很容易地关闭在 try-catch 语句块中使用的资源。资源(resource)是指在程序完成后,必须关闭的对象。try-with-resources 语句确保了每个资源在语句结束时关闭。所有实现了 java.lang.AutoCloseable 接口(其中,它包括实现了 java.io.Closeable 的所有对象),可以使用作为资源。


传统写法:try-catch-finally

public static void main(String[] args) {
    BufferedInputStream bin = null;
    BufferedOutputStream bout = null;
    try {
        bin = new BufferedInputStream(new FileInputStream(new File("")));
        bout = new BufferedOutputStream(new FileOutputStream(new File("")));
        while ((b = bin.read()) != -1) {
            bout.write(b);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (bin != null) {
            try {
                bin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

JDK 7:try-with-resources

public static void main(String[] args) {
    try (BufferedInputStream bin = new BufferedInputStream(new FileInputStream(new File("")));
         BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(new File("")))) {
        while ((b = bin.read()) != -1) {
            bout.write(b);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

JDK 9:
try-with-resources 声明在 JDK 9 已得到改进。如果你已经有一个资源是 final 或等效于 final 变量,您可以在 try-with-resources 语句中使用该变量,而无需在 try-with-resources 语句中声明一个新变量。

public static void main(String[] args) throws Exception {
    BufferedInputStream bin = new BufferedInputStream(new FileInputStream(new File("")));
    BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(new File("")));
    try (bin;bout) {
        while ((b = bin.read()) != -1) {
            bout.write(b);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

资源必须实现 AutoClosable 接口,并重写 close 方法

public class Connection implements AutoCloseable {
	@Override
	public void close(){
	}
}

相关推荐

  1. try-with-resources

    2024-07-18 01:04:04       22 阅读
  2. 记一次使用“try-with-resources“的语法导致的BUG

    2024-07-18 01:04:04       28 阅读
  3. 【Python- try 异常】

    2024-07-18 01:04:04       31 阅读
  4. C# —— try catch

    2024-07-18 01:04:04       15 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 01:04:04       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 01:04:04       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 01:04:04       58 阅读
  4. Python语言-面向对象

    2024-07-18 01:04:04       69 阅读

热门阅读

  1. 引领职场潮流,从这个霍兰德测试掌握先机!

    2024-07-18 01:04:04       20 阅读
  2. HG/T 3655-2024 紫外光UV固化木器涂料检测

    2024-07-18 01:04:04       19 阅读
  3. CBSD bhyve Ubuntu 配置vnc登录管理

    2024-07-18 01:04:04       21 阅读
  4. [USACO18JAN] Cow at Large P

    2024-07-18 01:04:04       19 阅读
  5. (78)组合环路--->(02)组合环路危害

    2024-07-18 01:04:04       21 阅读
  6. Apache Flume

    2024-07-18 01:04:04       25 阅读
  7. 编程参考 - 在C++移动构造函数声明中使用noexcept

    2024-07-18 01:04:04       23 阅读
  8. SQL概述及其规则与规范

    2024-07-18 01:04:04       25 阅读