這篇文章給大家分享的是有關怎樣在JDK 9中更簡潔使用try-with-resources語句的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

公司主營業(yè)務:成都網(wǎng)站設計、網(wǎng)站制作、外貿(mào)營銷網(wǎng)站建設、移動網(wǎng)站開發(fā)等業(yè)務。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)公司是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)公司推出東營區(qū)免費做網(wǎng)站回饋大家。
在 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 中執(zhí)行 close 以釋放資源
JDK 7 中的 try-with-resources 介紹
try-with-resources 是 JDK 7 中一個新的異常處理機制,它能夠很容易地關閉在 try-catch 語句塊中使用的資源。所謂的資源(resource)是指在程序完成后,必須關閉的對象。try-with-resources 語句確保了每個資源在語句結(jié)束時關閉。所有實現(xiàn)了 java.lang.AutoCloseable 接口(其中,它包括實現(xiàn)了 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");
}
}執(zhí)行輸出如下:
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 已得到改進。如果你已經(jīng)有一個資源是 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.
}感謝各位的閱讀!關于“怎樣在JDK 9中更簡潔使用try-with-resources語句”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
網(wǎng)頁名稱:怎樣在JDK9中更簡潔使用try-with-resources語句
當前地址:http://www.chinadenli.net/article16/piiedg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供Google、外貿(mào)網(wǎng)站建設、關鍵詞優(yōu)化、品牌網(wǎng)站建設、網(wǎng)站設計、云服務器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)