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问题的项 线程互斥----有点不懂,请教。。。 找错 MSSQLSERVER启动出问题 谁有工作流开发的源代码 一个带参数查询问题~! 请问中文乱码的解决方法 关于下面简单程序的提问? 请教java用Class.forName方法加载类后,如何卸载的问题? 一个可运行的java类在运行时位什么的到的类名不是原来的类名而时java.lang.Thread 如何在java程序中进行拨号? 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问题的项