您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“java中怎么使用try-with-resource實現(xiàn)輸入輸出流自動關(guān)閉”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
最近在做代碼審核的時候,審核工具提示我將 try-catch-finally 給替換掉,而且根據(jù)公司相關(guān)要求,該提示的級別還不低,不改不予通過。
FileReader fr = null; BufferedReader br = null; try { fr = new FileReader(fileName); br = new BufferedReader(fr); return br.readLine(); } catch (Exception e) { log.error("error:{}", e); } finally { if (br != null) { try { br.close(); } catch(IOException e){ log.error("error:{}", e); } } if (fr != null ) { try { br.close(); } catch(IOException e){ log.error("error:{}", e); } } }
try ( FileReader fr = new FileReader(fileName); BufferedReader br = new BufferedReader(fr) ) { return br.readLine(); }catch (Exception e) { log.error("error:{}", e); }
try ( BufferedReader br = new BufferedReader(new FileReader(fileName)) ) { // no need to name intermediate resources if you don't want to return br.readLine(); } catch (Exception e) { log.error("error:{}", e); }
對比代碼,不難發(fā)現(xiàn),輸入輸出流的關(guān)閉存在著差異。難道輸入輸出流不用關(guān)閉了嗎?
public class FileInputStream extends InputStream{} public abstract class InputStream implements Closeable {} /** * A {@code Closeable} is a source or destination of data that can be closed. * The close method is invoked to release resources that the object is * holding (such as open files). * * @since 1.5 */ public interface Closeable extends AutoCloseable {} /** * An object that may hold resources (such as file or socket handles) * until it is closed. The {@link #close()} method of an {@code AutoCloseable} * object is called automatically when exiting a {@code * try}-with-resources block for which the object has been declared in * the resource specification header. This construction ensures prompt * release, avoiding resource exhaustion exceptions and errors that * may otherwise occur. * * @apiNote * <p>It is possible, and in fact common, for a base class to * implement AutoCloseable even though not all of its subclasses or * instances will hold releasable resources. For code that must operate * in complete generality, or when it is known that the {@code AutoCloseable} * instance requires resource release, it is recommended to use {@code * try}-with-resources constructions. However, when using facilities such as * {@link java.util.stream.Stream} that support both I/O-based and * non-I/O-based forms, {@code try}-with-resources blocks are in * general unnecessary when using non-I/O-based forms. * * @author Josh Bloch * @since 1.7 */ public interface AutoCloseable {}
AutoCloseable 顧名思義, 自動關(guān)閉流. 從注釋中我們可以發(fā)現(xiàn),實現(xiàn)了AutoCloseable并在try()中聲明的對象,當try-with-resource代碼塊執(zhí)行完的時候,會自動調(diào)用close()方法。
注意:
一個 try-with-resources 語句可以像普通的 try 語句那樣有 catch 和 finally 塊。在try-with-resources 語句中, 任意的 catch 或者 finally 塊都是在聲明的資源被關(guān)閉以后才運行。
try-with-resource是JDK7引入的語法糖,可以簡化Autocloseable資源類的關(guān)閉過程,
File file = new File("d:/tmp/1.txt"); FileInputStream fis = null; try { fis = new FileInputStream(file); xxxxx xxxxx } catch (IOException e) { e.printStackTrace(); }finally{ if(fis != null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } }
上面是一段讀取文件內(nèi)容的示意代碼,為了防止在try代碼塊中出現(xiàn)異常后導(dǎo)致的資源泄露問題,在finally代碼塊中一般處理資源的關(guān)閉事項。
File file = new File("d:/tmp/1.txt"); try(FileInputStream fis = new FileInputStream(file);) { fis.read(); } catch (IOException e) { e.printStackTrace(); }finally{ }
可以看出是簡化了不少,之所以稱之為語法糖,是因為編譯成class文件后實際的代碼就不是這樣的了,編譯過程中會自動添加資源的關(guān)閉處理。
File file = new File("d:/tmp/1.txt"); try { Throwable var2 = null; Object var3 = null; try { FileInputStream fis = new FileInputStream(file); xxx xxxx } catch (Throwable var12) { if (var2 == null) { var2 = var12; } else if (var2 != var12) { var2.addSuppressed(var12); } throw var2; } } catch (IOException var13) { var13.printStackTrace(); }
好了,上面已經(jīng)引入今天的主題,try-with-resource,但是仍然有需要注意的地方。
private static class MyResource implements AutoCloseable{ private MyResource1 res; public MyResource(MyResource1 res){ this.res = res; } @Override public void close() throws Exception { System.out.println("MyResource自動關(guān)閉"); Integer a = null; a.toString(); this.res.close(); } } private static class MyResource1 implements AutoCloseable{ @Override public void close() throws Exception { System.out.println("MyResource1自動關(guān)閉"); } } @Test public void test() throws Exception{ try( MyResource r = new MyResource(new MyResource1())){ Integer a = null ; a.toString(); } }
執(zhí)行上面的代碼,由于MyResource的close方法中出現(xiàn)了異常,此時創(chuàng)建的MyResource1就不會被關(guān)閉,從而出現(xiàn)資源泄露情況,為了規(guī)避這個問題,為了規(guī)避這個問題,我們需要創(chuàng)建的實現(xiàn)AutoCloseable接口的對象單獨創(chuàng)建。
try( MyResource1 res= new MyResource1(); MyResource r = new MyResource(res)){ Integer a = null ; a.toString(); }
“java中怎么使用try-with-resource實現(xiàn)輸入輸出流自動關(guān)閉”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。