您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)怎樣在JDK 9中更簡潔使用try-with-resources語句的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
在 JDK 7 之前,資源需要手動關(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)閉的對象。try-with-resources 語句確保了每個(gè)資源在語句結(jié)束時(shí)關(guān)閉。所有實(shí)現(xiàn)了 java.lang.AutoCloseable 接口(其中,它包括實(shí)現(xiàn)了 java.io.Closeable 的所有對象),可以使用作為資源。
例如,我們自定義一個(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
可以看到,資源終止被自動關(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. }
感謝各位的閱讀!關(guān)于“怎樣在JDK 9中更簡潔使用try-with-resources語句”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。