![](/img/trans.png)
[英]java.io.IOException: read failed: EBADF (Bad file descriptor)
[英]java.io.IOException: Failed to delete original file moveFileToDirectory
轉換文件后,我嘗試使用FileUtils.moveFileToDirectory在if語句中移動以下文件。 JPG和gif文件將移至新文件夾,但是只要程序找到ICO文件,它都不會將該文件移至新文件夾並為您提供StackTrace:java.io.IOException:無法刪除原始文件“原始路徑”復制到“文件的新路徑”之后。 這是該方法的代碼:
public void storeOriginalImages() {
for(File file: model.getFileList()) {
if(file.getName().endsWith(".GIF") || file.getName().endsWith(".gif") || file.getName().endsWith(".JPG")
|| file.getName().endsWith(".jpg") || file.getName().endsWith(".ico") || file.getName().endsWith(".ICO")
|| file.getName().endsWith(".BMP") || file.getName().endsWith(".bmp")) {
System.out.println("in copy else");
File illegalExtension = new File(file.getAbsolutePath());
File illegalExtensionDest = new File(model.getTargetexcelFilepath() + "/" + model.getFolderName() + "_img_backup");
System.out.println(illegalExtension + "/" + illegalExtensionDest);
try {
FileUtils.moveFileToDirectory(illegalExtension, illegalExtensionDest, true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
這是將ICO文件轉換為png的方式:
else if(s.getName().endsWith(".ico") || s.getName().endsWith(".ICO")) {
List<BufferedImage> bi = ICODecoder.read(new File(s.getAbsolutePath()));
System.out.println("reading");
ImageIO.write(bi.get(0), "png", new File(s.getParentFile().getAbsoluteFile(), fileNameWithOutExt + ".png"));
System.out.println("Ico was converted.");
}
我以您的示例為例,並進行了一些編輯。 似乎當ICODecoder嘗試使用流讀取文件時,它沒有正確關閉它,因此您需要在代碼中關閉它。 這是工作示例
File oldFile = new File("a.ico");
try (InputStream inputStream = new FileInputStream(oldFile)) {
List<BufferedImage> bi = ICODecoder.read(inputStream);
ImageIO.write(bi.get(0), "png", new File("a" + ".png"));
} catch (IOException e) {
LOG.error("Something happend", e);
}
FileUtils.moveFile(oldFile, new File("a.jpg"));
您需要先關閉輸入流,然后再移動文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.