JDK7 AutoCloseable

干嘛的

直接看JDK7的流(运用了AutoCloseable)源码

public abstract class InputStream implements Closeable {
     //实现Closeable接口中的close方法
     public void close() throws IOException {}
}

public interface Closeable extends AutoCloseable {
    public void close() throws IOException;
}

JDK7起,无需我们在finally中进行流的关闭,原因在此

try(ObjectInputStream ois = new ObjectInputStream();) {}

拓展

当我们使用其他(需要关闭的)资源时,也可以实现AutoCloseable

posted @ 2019-02-12 11:34  Mr.Zhongzz  阅读(288)  评论(0)    收藏  举报