fileoutputstream的关闭问题 本帖最后由 aotian16 于 2011-07-28 12:41:22 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 findbug没用过。 你运行一下嘛不测试怎么知道是什么问题列。肉眼谁看的出来 findbugs插件找的是静态的可能出现bug的代码啊,运行也看不出来问题 要说问题,可能也就是 inStream.close(); 如果异常,fs没关掉。你把inStream.close(); 的 throw e1 删掉就得了,关闭失败就失败呗。 我也用findBugs试了下。。跑了一下代码,发现没有问题。就是提示了一下注释,以及 byte[] buffer = new byte[1444]; 你这么一说我还真有点眉目了前面inStream关闭失败的话, throw出去后面就执行不了了, 所以有close的问题 这样就OK了 /** * * * @param oldPath * * @param newPath * * @throws IOException */ private static void copyFile(String oldPath, String newPath) throws IOException { int bytesum = 0; int byteread = 0; File oldfile = new File(oldPath); InputStream inStream = null; FileOutputStream fs = null; try { if (oldfile.exists()) { inStream = new FileInputStream(oldPath); try { fs = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; while ((byteread = inStream.read(buffer)) != -1) { bytesum += byteread; fs.write(buffer, 0, byteread); } } catch (IOException e) { throw e; } finally { if (fs != null) { try { fs.close(); } catch (IOException e1) { throw e1; } } } } } catch (IOException e) { throw e; } finally { if (inStream != null) { try { inStream.close(); } catch (IOException e1) { throw e1; } } } }参考了这个帖子, 一篇好文,写得比较幽默,看者有分!很久以前看到过, 现在回想起来, 确实有用 4楼说的有道理,不过我也findBugs了一下,结果没有发现问题。是我版本太老了吗? 可能不是的,因为我用的findbugs我们公司改造过的里面就有针对这个close问题的项 有没有人知道,异常的信息为什么是空!!! 设置了容器的背景颜色为红色,容器的背景颜色不改变 一个java线程问题,麻烦大家看下 关于java导出xml文件的问题 excel文件打开后再导入数据报错 能直接覆盖吗 请问如何控制线程并发的个数? 求助java:查询数据库后返回数据量过大,怎么才能提高传输效率 新手上路,各位大侠再帮帮忙 UlraEdit? servlet 同 applet 通讯问题 java log4j配置文件位置 关于integer类型的变量的问题!!!求解!
运行也看不出来问题
就是提示了一下注释,以及 byte[] buffer = new byte[1444];
前面inStream关闭失败的话, throw出去
后面就执行不了了, 所以有close的问题
/**
*
*
* @param oldPath
*
* @param newPath
*
* @throws IOException
*/
private static void copyFile(String oldPath, String newPath)
throws IOException { int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
InputStream inStream = null;
FileOutputStream fs = null;
try {
if (oldfile.exists()) {
inStream = new FileInputStream(oldPath);
try {
fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
fs.write(buffer, 0, byteread);
}
} catch (IOException e) {
throw e;
} finally {
if (fs != null) {
try {
fs.close();
} catch (IOException e1) {
throw e1;
}
}
}
}
} catch (IOException e) {
throw e;
} finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException e1) {
throw e1;
}
}
}
}参考了这个帖子, 一篇好文,写得比较幽默,看者有分!
很久以前看到过,
现在回想起来, 确实有用
因为我用的findbugs我们公司改造过的
里面就有针对这个close问题的项