不用写一大堆finally来关闭资源,所有实现Closeable的类声明都可以写在里面,最常见于流操作,socket操作,新版的httpclient也可以;
需要注意的是,try()的括号中可以写多行声明,每个声明的变量类型都必须是Closeable的子类,用分号隔开
try (
ZipOutputStream out=new ZipOutputStream(new FileOutputStream(s));
){
out.putNextEntry(new ZipEntry("1239000.txt"));
Files.copy(Paths.get(fileStr),out);
}catch (Exception e){
e.printStackTrace();
}