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

posted @ 2021-03-12 21:35  egu0o  阅读(1258)  评论(0编辑  收藏  举报