file.delete删除文件失效,也不报错

今天遇到一个问题,就是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();
			}
		}
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值