如何在JDK 9中更簡(jiǎn)潔使用 try-with-resources 語句
在 JDK 7 之前,資源需要手動(dòng)關(guān)閉
例如下面一個(gè)很常見的文件操作的例子:
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 中執(zhí)行 close 以釋放資源
JDK 7 中的 try-with-resources 介紹
try-with-resources 是 JDK 7 中一個(gè)新的異常處理機(jī)制,它能夠很容易地關(guān)閉在 try-catch 語句塊中使用的資源。所謂的資源(resource)是指在程序完成后,必須關(guān)閉的對(duì)象。try-with-resources 語句確保了每個(gè)資源在語句結(jié)束時(shí)關(guān)閉。所有實(shí)現(xiàn)了 java.lang.AutoCloseable 接口(其中,它包括實(shí)現(xiàn)了 java.io.Closeable 的所有對(duì)象),可以使用作為資源。
例如,我們自定義一個(gè)資源類
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");
}
}
執(zhí)行輸出如下:
do something resource is closed
可以看到,資源終止被自動(dòng)關(guān)閉了。
再來看一個(gè)例子,是同時(shí)關(guān)閉多個(gè)資源的情況:
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 語句中越是最后使用的資源,越是最早被關(guān)閉。
try-with-resources 在 JDK 9 中的改進(jìn)
作為 Milling Project Coin 的一部分, try-with-resources 聲明在 JDK 9 已得到改進(jìn)。如果你已經(jīng)有一個(gè)資源是 final 或等效于 final 變量,您可以在 try-with-resources 語句中使用該變量,而無需在 try-with-resources 語句中聲明一個(gè)新變量。
例如,給定資源的聲明
// 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.
}
看上去簡(jiǎn)潔很多吧。對(duì) Java 未來的發(fā)展信心滿滿。
愿意嘗試 JDK 9 這種新語言特性的可以下載使用 JDK 9 快照。Enjoy!
源碼
本章例子的源碼,可以在 https://github.com/waylau/essential-java 中 com.waylau.essentialjava.exception.trywithresources 包下找到。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于JSON和java對(duì)象的互轉(zhuǎn)方法
下面小編就為大家?guī)硪黄贘SON和java對(duì)象的互轉(zhuǎn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
EasyExcel實(shí)現(xiàn)讀寫Excel文件的示例代碼
EasyExcel是阿里巴巴開源的一個(gè)excel處理框架,以使用簡(jiǎn)單、節(jié)省內(nèi)存著稱。它可以在盡可能節(jié)約內(nèi)存的情況下支持讀寫百M(fèi)的Excel,所以本文就將利用它實(shí)現(xiàn)讀寫Excel文件,感興趣的可以了解一下2022-08-08
Java通過百度API實(shí)現(xiàn)圖片車牌號(hào)識(shí)別
這段時(shí)間做項(xiàng)目需要用java程序進(jìn)行車牌識(shí)別,因此嘗試做了下這個(gè)程序,本代碼功能是通過調(diào)用百度API實(shí)現(xiàn)的,感興趣的可以了解一下2021-06-06
使用mybatis框架連接mysql數(shù)據(jù)庫的超詳細(xì)步驟
MyBatis是目前java項(xiàng)目連接數(shù)據(jù)庫的最流行的orm框架了,下面這篇文章主要給大家介紹了關(guān)于使用mybatis框架連接mysql數(shù)據(jù)庫的超詳細(xì)步驟,文中通過實(shí)例代碼和圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
spring boot請(qǐng)求異常處理并返回對(duì)應(yīng)的html頁面
這篇文章主要介紹了spring boot處理請(qǐng)求異常并返回對(duì)應(yīng)的html頁面,包括404異常處理和500異常處理,需要的朋友可以參考下2017-07-07
SpringBoot配置文件bootstrap和application區(qū)別及說明
這篇文章主要介紹了SpringBoot配置文件bootstrap和application區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
簡(jiǎn)單分析Java線程編程中ThreadLocal類的使用
這篇文章主要介紹了Java線程編程中ThreadLocal類的使用,包括使用其對(duì)共享變量的操作的分析,需要的朋友可以參考下2015-12-12

