首页 新闻 会员 周边

求帮忙解决Java中io流关闭时候的block问题

0
悬赏园豆:5 [已关闭问题] 关闭于 2019-04-04 14:43

public static void copyFolder(String oldPath, String newPath) {
FileInputStream input = null;
FileOutputStream output = null;
try {
new File(newPath).mkdirs(); // 如果文件夹不存在 则建立新文件夹
File a = new File(oldPath);
String[] file = a.list();
File temp = null;
for (int i = 0; i < file.length; i++) {
if (oldPath.endsWith(File.separator)) {
temp = new File(oldPath + file[i]);
} else {
temp = new File(oldPath + File.separator + file[i]);
}
if (temp.isFile()) {
input = new FileInputStream(temp);
output = new FileOutputStream(newPath
+ "/" + (temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();

            }
            if (temp.isDirectory()) {// 如果是子文件夹
                copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (null != input) {
                input.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        try {
            if (null != output) {
                output.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

用sonarLint 4.0版本代码走查,一直显示input没有关闭。可是我真的已经关了啊

Resource Date Description
FileUtil.java 2 hours ago Use try-with-resources or close this "FileInputStream" in a "finally" clause.

Co~Co的主页 Co~Co | 小虾三级 | 园豆:507
提问于:2019-03-26 13:52

没有人能回答吗?output被关闭了,但是input没有被关闭。。。

coco_xu 5年前
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册