改findbogs碰到的两个问题,一个是关于IO流,一个是关于空指针检查异常。

1.NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE

前面代码略。。。

 File crFile = new File(crFilelocalPath);
if (crFile.exists()&& crFile.isDirectory() && crFile.listFiles() != null
&& crFile.listFiles().length > 0) { //略。。。 } 后面略。。。。

原因分析:

问题出在 crFile.listFiles() ,google到,说是第一个 crFile.listFiles()判断不为null,但是第二个crFile.listFiles()还是可能会为null。(原文:If the first call of listFiles() is != null, that doesn't mean the second call is also != null.)

google原文见:https://sourceforge.net/p/findbugs/bugs/1468/

所以改为

前面略。。

     File crFile = new File(crFilelocalPath);
if (crFile.exists() && crFile.isDirectory()) {
File[] files = crFile.listFiles();
if (files != null && files.length > 0) {
//略。。。
}
}
后面略。。。

findbugs就不报错了。。。

2.OBL_UNSATISFIED_OBLIGATION

是关于IO流的关闭

findbugs报错的代码

 public static boolean storeFile2LocalPath(String fileName, String localPath, File fileInput) {
boolean success = false;
FileInputStream inputStream = null;
OutputStream os = null;
try {
inputStream = new FileInputStream(fileInput);
//保存到临时文件
byte[] bs = new byte[1024];// 1K的数据缓冲
int len;// 读取到的数据长度
// 输出的文件流保存到本地文件
File tempFile = new File(localPath);
if (!tempFile.exists()) {
tempFile.mkdirs();
}
String filePath = tempFile.getPath() + File.separator + fileName;
os = new FileOutputStream(filePath);
log.info("storeFile2LocalPath() and filePath = " + filePath);
// 开始读取
while ((len = inputStream.read(bs)) != -1) {
os.write(bs, 0, len);
}
success = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
// 完毕,关闭所有链接
try {
if(os != null) {
os.close();
os = null;
}
if(inputStream != null){
inputStream.close();
inputStream = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return success;
}

报错如下:

This method may fail to clean up (close, dispose of) a stream, 
database object, or other resource requiring an explicit cleanup 
operation.

In general, if a method opens a stream or other resource, the method 
should use a try/finally block to ensure that the stream or resource 
is cleaned up before the method returns.

This bug pattern is essentially the same as the **OS_OPEN_STREAM and 
ODR_OPEN_DATABASE_RESOURCE** bug patterns, but is based on a different 
(and hopefully better) static analysis technique. We are interested is 
getting feedback about the usefulness of this bug pattern. To send 
feedback, either: •send email to findbugs@cs.umd.edu •file a bug 
report: http://findbugs.sourceforge.net/reportingBugs.html

In particular, the false-positive suppression heuristics for this bug 
pattern have not been extensively tuned, so reports about false 
positives are helpful to us.

See Weimer and Necula, Finding and Preventing Run-Time Error Handling 
Mistakes, for a description of the analysis technique.

原因分析:

连续关闭两个流,在同一个finally快里,若第一个流close失败,出现异常时,会导致第二个流没有关闭。

所以改为如下方式,findbugs不报错了。

//将后面的finally块改为: 再嵌套一个finally块

finally {
// 完毕,关闭所有链接
try {
if (os != null) {
os.close();
os = null;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
inputStream = null;
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}

最新文章

  1. ExtJS 4.2 业务开发(一)主页搭建
  2. PHP的学习--RSA加密解密
  3. 移动web
  4. Linux文件标示
  5. Redis主从是否生效的特殊测试方法
  6. PHP获取当前日期或指定日期的所在月的第一天和最后一天
  7. switchomega配置
  8. C#面向对象的学习笔记
  9. 生产环境搭建MySQL复制的教程(转)
  10. 设计模式--委托模式C++实现
  11. SQL 语句优化—— (二) 索引的利用
  12. Office文档在线编辑的实现之一
  13. java 构造器学习笔记
  14. 为Arch Linux安装搜狗输入法
  15. inline-block的理解
  16. Hbase学习之概念与原理
  17. 【Alpha 冲刺】 1/12
  18. Ubantu里面的Sublime Text3不支持中文的解决办法
  19. python爬虫-基础
  20. 核心重点lxml

热门文章

  1. 多线程-join()方法
  2. vivado设计一:建立第一个入门工程(基于zybo)
  3. JavaScript No Overloading 函数无重载之说
  4. Controller Service Dao总结
  5. InnoDB:文件
  6. 创建ajax的过程
  7. 使用ss命令代替 netstat
  8. PHP学习笔记(13)班级和学生管理---班级
  9. spoj7001 Visible Lattice Points 莫比乌斯反演+三维空间互质对数
  10. easyui.dialog.js