1.问题场景
最近收集了好多壁纸,有好多图片分辨率不是1920x1080
比例的,就用Java
写了个小程序筛选出合适比例的图片(代码在文末),结果在删除图片时使用File
类的delete()
方法返回的是false
,即表示文件无法删除。
2.原因及解决方法
由于需要判断图片文件的分辨率比例,所以使用了ImageIO
类的read()
方法来获得图片文件的分辨率,代码如下:
BufferedImage source = ImageIO.read(new FileInputStream(pic)); //pic是指图片文件
因此在其后使用File
的delete()
方法删除图片时就会返回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.");
}
}
}
}
}
}