該接口用於try-with-resources語法糖提供支持,用於自動關閉資源做用code
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去關閉資源了,方便快捷 } }