今天公司sonar扫描出之前的项目漏洞以及bug,之前项目bug漏洞真不少,其中就有个
Use try-with-resources or close this "****" in a "finally" clause.
提示我们需要关闭或者使用try-with-resources
正常以前写法习惯在finally中close掉由于在try中还得判断对象是否为null,不为null在close,判断复杂,
现在有种更优雅的方式
try(
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
InputStream in1 = new BufferedInputStream(new FileInputStream(filePath));
){
}catch(Exception e){
}
括号里面可以写多个,其实也算是语法糖了,反编译看一下是帮我写好了close,具体更多案例大家可以上网上找找看看,这里面简单记录下。