JDK8新特性之 try-with-resources
(供笔者学习记录)
什么是try-with-resources
- 资源的关闭很多⼈停留在旧的流程上,jdk7新特性就有,但是很多⼈以为是jdk8的 在try( …)⾥声
明的资源,会在try-catch代码块结束后⾃动关闭掉
- 旧的方式:
public static void main(String[] args) throws IOException {
String path = "/Users/a.txt";
test(path);
}
private static void test(String filepath) throws FileNotFoundException {
OutputStream out = new FileOutputStream(filepath);
try