在 JDK 9 中更简洁使用 try-with-resources 语句

简介:

本文详细介绍了自 JDK 7 引入的 try-with-resources 语句的原理和用法,以及介绍了 JDK 9 对 try-with-resources 的改进,使得用户可以更加方便、简洁的使用 try-with-resources 语句。

在 JDK 7 之前,资源需要手动关闭。

例如下面一个很常见的文件操作的例子:

Charset charset = Charset.forName("US-ASCII");
String s = ...;
BufferedWriter writer = null;
try {
 writer = Files.newBufferedWriter(file, charset);
 writer.write(s, 0, s.length());
} catch (IOException x) {
 System.err.format("IOException: %s%n", x);
} finally {
 if (writer != null) writer.close();
}

在 JDK 7 之前,你一定要牢记在 finally 中执行 close 以释放资源

JDK 7 中的 try-with-resources 介绍

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

例如,我们自定义一个资源类

public class Demo { 
 public static void main(String[] args) {
 try(Resource res = new Resource()) {
 res.doSome();
 } catch(Exception ex) {
 ex.printStackTrace();
 }
 }
}

class Resource implements AutoCloseable {
 void doSome() {
 System.out.println("do something");
 }
 @Override public void close() throws Exception {
 System.out.println("resource is closed");
 }
}

执行输出如下:

do something
resource is closed

可以看到,资源终止被自动关闭了。

再来看一个例子,是同时关闭多个资源的情况:

public class Main2 { 
 public static void main(String[] args) {
 try(ResourceSome some = new ResourceSome();
 ResourceOther other = new ResourceOther()) {
 some.doSome();
 other.doOther();
 } catch(Exception ex) {
 ex.printStackTrace();
 }
 }
}

class ResourceSome implements AutoCloseable {
 void doSome() {
 System.out.println("do something");
 }
 @Override public void close() throws Exception {
 System.out.println("some resource is closed");
 }
}

class ResourceOther implements AutoCloseable {
 void doOther() {
 System.out.println("do other things");
 }
 @Override public void close() throws Exception {
 System.out.println("other resource is closed");
 }
}

最终输出为:

do something
do other things
other resource is closed
some resource is closed

在 try 语句中越是最后使用的资源,越是最早被关闭。

try-with-resources 在 JDK 9 中的改进

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

例如,给定资源的声明

// A final resource final Resource resource1 = new Resource("resource1");
// An effectively final resource
Resource resource2 = new Resource("resource2");

老方法编写代码来管理这些资源是类似的:

// Original try-with-resources statement from JDK 7 or 8 try (Resource r1 = resource1;
 Resource r2 = resource2) {
 // Use of resource1 and resource 2 through r1 and r2.
}

而新方法可以是

// New and improved try-with-resources statement in JDK 9 try (resource1;
 resource2) {
 // Use of resource1 and resource 2.
}

看上去简洁很多吧。对 Java 未来的发展信心满满。

愿意尝试 JDK 9 这种新语言特性的可以下载使用 JDK 9 快照。Enjoy!

源码

本章例子的源码,可以在 https://github.com/waylau/essential-javacom.waylau.essentialjava.exception.trywithresources 包下找到。

参考

目录
相关文章
|
开发工具 git
记IDEA Git版本回退并push到远程操作
记IDEA Git版本回退并push到远程操作
406 1
记IDEA Git版本回退并push到远程操作
|
存储 自然语言处理 Oracle
打造全球化企业:ERP系统的国际化与多语言支持
打造全球化企业:ERP系统的国际化与多语言支持
841 2
|
安全 Unix Linux
在Python中,如何处理文件和目录的访问权限?
【2月更文挑战第15天】【2月更文挑战第43篇】在Python中,如何处理文件和目录的访问权限?
511 0
|
7月前
|
机器学习/深度学习 运维 自然语言处理
大模型技术在安全威胁检测中的应用:从传统到未来的跃升
大模型技术在安全威胁检测中的应用:从传统到未来的跃升
573 1
大模型技术在安全威胁检测中的应用:从传统到未来的跃升
|
缓存 JavaScript
Error: EPERM: operation not permitted, mkdir ‘C:\Program Files\nodejs‘TypeError: Cannot read proper
Error: EPERM: operation not permitted, mkdir ‘C:\Program Files\nodejs‘TypeError: Cannot read proper
278 0
7、Mybatis-Plus condition的使用
这篇文章介绍了Mybatis-Plus中`condition`的使用,展示了如何通过条件性地组装查询条件来构建SQL查询,以及如何使用`LambdaQueryWrapper`和`LambdaUpdateWrapper`来防止字段错误并增强查询和更新操作的安全性和便捷性。
|
设计模式 XML Java
Spring的生命周期及Spring Bean单例和多例---超详细教学
Spring的生命周期及Spring Bean单例和多例---超详细教学
1166 0
|
SQL Java 关系型数据库
MyBatis的动态SQL之OGNL(Object-Graph Navigation Language)表达式以及各种标签的用法
MyBatis的动态SQL之OGNL(Object-Graph Navigation Language)表达式以及各种标签的用法
322 0
|
安全 Java API
JDK 9中try-with-resources的改进:提高资源管理的效率和安全性
本文将深入探讨JDK 9中try-with-resources的新特性及其改进。try-with-resources语句用于自动关闭实现了AutoCloseable接口的资源,如文件、数据库连接等。在JDK 9中,try-with-resources得到了进一步的优化和改进,提高了资源管理的效率和安全性。本文将详细介绍这些改进,以及如何利用这些改进来提高代码质量和应用程序性能。
|
SQL Java 数据库连接
Spring Boot 学习研究笔记(九) - Spring Data JPA常用注解(2)
Spring Boot 学习研究笔记(九) - Spring Data JPA常用注解
374 0