Java中的 try(){}


從網上查閱資料得知從 Java 7 build 105 版本開始,Java 7 的編譯器和運行環境支持新的 try-with-resources 語句,稱為 ARM 塊(Automatic Resource Management) ,自動資源管理:

The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

谷歌翻譯:

try-with-resources 語句是聲明一個或多個資源的 try 語句。 資源是一個對象,程序完成后必須將其關閉。 try-with-resources 語句可確保在語句末尾關閉每個資源。 任何實現 java.lang.AutoCloseable 的對象(包括所有實現 java.io.Closeable 的對象)都可以用作資源。


//src
public abstract class InputStream implements Closeable {

    // MAX_SKIP_BUFFER_SIZE is used to determine the maximum buffer size to
    // use when skipping.
    private static final int MAX_SKIP_BUFFER_SIZE = 2048;

    
    //...
}

參考:https://blog.csdn.net/qq_33543634/article/details/80725899


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM