java7 的编译和运行环境支持 try-with-resources语句,称为automatic resources management, 自动资源管理。
public static void downloadCommonBundle(Configuration conf,String srcFile,String destFile) {
try (FileOperation operation = new FileOperation(conf)) {
operation.downloadFile(srcFile, destFile);
} catch (Exception e) {
logger.error("downloadCommonBundle",e);
}
}
数据流会在try执行完毕后自动关闭,不需要再去close。但是这些资源要实现java.lang.AutoCloseable 接口。
public class FileOperation implements AutoCloseable {}