今天遇到一个问题,就是java删除文件的时候,删除不了,也不报错。
参考:https://www.cnblogs.com/wuyun-blog/p/9468117.html
我就是情况3.
找了老半天,发现是finally里面关重复了,导致漏关了一个。。。。。
public void copyFile(File in, String file2,String imgName) throws Exception{
File out = new File(file2);
//目标文件夹
if (!out.exists()) {
out.mkdirs();
}
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//创建读取 要拷贝的文件
fis = new FileInputStream(in);
//创建 要复制到的文件
logger.info("file: {}",file2+ "/" + imgName);
fos = new FileOutputStream(new File(file2+ "/" + imgName));
int c;
//创建字节数组
byte[] b = new byte[1024 * 5];
//写入文件
while ((c = fis.read(b)) != -1) {
fos.write(b, 0, c);
}
} catch (FileNotFoundException e) {
logger.error("拷贝文件出现异常!新文件名:{}",imgName,e);
//throw new FileNotFoundException();
} catch (IOException e) {
logger.error("拷贝文件出现异常!新文件名:{}",imgName,e);
//throw new FileNotFoundException();
}finally {
if(fis != null){
fis.close();
}
if(fos != null){
fos.close();
}
}
}