如何将以下代码转换为JAVA 8 - 尝试使用资源。我已经在Java 6中这样写代码,我已经升级的Java 6到Java 8 - 声纳给拦截消息“尝试 - 以资源应使用”应尝试使用资源
public void archivingTheFile(String zipFile){
byte[] buffer = new byte[1024];
try{
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
for(String file : this.fileList){
ZipEntry ze= new ZipEntry(file);
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(SOURCE_FOLDER + File.separator + file);
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
}
zos.closeEntry();
zos.close();
}catch(IOException ex){
LOGGER.error("Exception occurred while zipping file",ex);
}
}
参考:https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html – gba