该接口用于try-with-resources语法糖提供支持,用于自动关闭资源作用
close方法最后调用的基本上都是本地方法。例子:
public class AutoCloseableTest {
@Test
public void test(){
try(FileInputStream inputStream=new FileInputStream(new File("test.txt"))){
//do somethings
} catch (IOException e) {
e.printStackTrace();
}
//不需要在使用finally去关闭资源了,方便快捷
}
}
原文:https://www.cnblogs.com/lonecloud/p/9102948.html