释放资源的方式try-with-resources

1.try-catch-finally

2.try-with-resources

使用方法

try(//这里定义你要使用的资源){}
catch(){}

注意:try()里只能存放流对象(资源对象),什么是资源呢?就是会自动实现AutoCloseable接口

使用2方法时会在资源使用完毕后自动对其释放close

posted @ 2023-12-09 16:39  天启A  阅读(22)  评论(0)    收藏  举报