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.
没有人能回答吗?output被关闭了,但是input没有被关闭。。。
– coco_xu 5年前