问题解决:Java通过File类的delete方法删除文件返回false

Java通过File类的delete方法删除文件返回false

1.问题场景

最近收集了好多壁纸,有好多图片分辨率不是1920x1080比例的,就用Java写了个小程序筛选出合适比例的图片(代码在文末),结果在删除图片时使用File类的delete()方法返回的是false,即表示文件无法删除。

2.原因及解决方法

由于需要判断图片文件的分辨率比例,所以使用了ImageIO类的read()方法来获得图片文件的分辨率,代码如下:

BufferedImage source = ImageIO.read(new FileInputStream(pic));	//pic是指图片文件

因此在其后使用Filedelete()方法删除图片时就会返回false,即图片此时被FileInputStream流读取,而且这个流并没有关闭。
解决方法就是手动关闭这个流,代码如下:

try{
	FileInputStream inputStream = new FileInputStream(pic);
	BufferedImage source = ImageIO.read(inputStream);
	//获取图片的分辨率等操作
	...
} catch(...) {
	//异常处理
	...
} finally {
	if(inputStream != null) {
		try{
			inputStream.close();	//手动关闭
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
//删除文件操作
...

3.代码

package otheruse;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

import javax.imageio.ImageIO;

public class GetResolutionRatio {
	
	/*
	 * 使用FileChannel类复制文件
	 * @param source	源文件
	 * @param dest		目标文件
	 */
	private static void copyFilesUsingFileChannels(File source, File dest) throws IOException {
		FileChannel inputChannel = null;
		FileChannel outputChannel = null;
		try {
			inputChannel = new FileInputStream(source).getChannel();
			outputChannel = new FileOutputStream(dest).getChannel();
			outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
		} finally {
			inputChannel.close();
			outputChannel.close();
		}
	}

	/*
	 * main方法
	 */
	public static void main(String[] args) {
		String path = "E:\\usr\\Pictures\\Saved Pictures";		//图片文件夹路径
		File dir = new File(path);
		if (dir.isDirectory()) {
			String[] fileList = dir.list();						//读取文件夹中的所有文件
			for (int i = 0; i < fileList.length; i++) {
				File pic = new File(path + "\\" + fileList[i]);	//为每一个文件实例化
				boolean deleted = false;
				BufferedImage sourceImg = null;
				FileInputStream inputStream = null;
				try {
					inputStream = new FileInputStream(pic);		//使用ImageIO类的read方法读取图片文件
					sourceImg = ImageIO.read(inputStream);
					if (sourceImg == null) {
						System.err.println(fileList[i]);
					} else if (sourceImg.getWidth() == 1920 && sourceImg.getHeight() == 1080) {
						System.out.println(fileList[i]);		//打印出满足1920x1080分辨率的图片名称
						File destfFile = new File("E:\\usr\\Pictures\\Camera Roll\\background\\" + fileList[i]);	//指定复制的位置
						copyFilesUsingFileChannels(pic, destfFile);		//复制到指定位置
						deleted = true;
					} else if (sourceImg.getWidth() > 1920 && sourceImg.getHeight() > 1080) {
						if (sourceImg.getWidth() * 1080 == sourceImg.getHeight() * 1920) {
							System.out.println(fileList[i]);	//打印出满足1920:1080比例的图片名称
							File destFile = new File("E:\\usr\\Pictures\\Camera Roll\\background\\" + fileList[i]);	//指定复制到位置
							copyFilesUsingFileChannels(pic, destFile);	//复制到指定位置
							deleted = true;
						}
					}
				} catch (FileNotFoundException e) {
					e.printStackTrace();
					System.err.println(fileList[i]);
				} catch (IOException e) {
					e.printStackTrace();
					System.err.println(fileList[i]);
				} finally {
					if(inputStream != null) {
						try {
							inputStream.close();	//关闭流
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
				//删除源文件
				if(deleted) {
					if(pic.delete()) {
						System.out.println("Delete successfully.");
					} else {
						System.out.println("Delete failed.");
					}
				}
			}
		}
	}
}

4.参考文章

Close BufferedImage so I can delete it?
java复制文件的4种方式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值