用Windows自带的图像查看器打开是张空白没有预览的图片,但是用Picasa照片查看器可以看到是一张气象图片,用ImageIO.read这文件的时候就报错,我是想循环批量读取文件夹里面的所有图片,但是每次读到这张图的时候就报错,后面的都无法读取了,该怎么办?
- Java code
import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class BufferedImageTest { /** * @param args */ public static void main(String[] args) { File file =new File("E://star201208091530.gif"); BufferedImage image=null; try { image=ImageIO.read(new File("F://"+file.getName())); ImageIO.write(image,file.getName().split(".")[1] , file); } catch (IOException e) { e.printStackTrace(); } } }
报这样的错误:
Exception in thread "main" java.lang.IndexOutOfBoundsException at java.io.RandomAccessFile.readBytes(Native Method) at java.io.RandomAccessFile.read(RandomAccessFile.java:322) at javax.imageio.stream.FileImageInputStream.read(FileImageInputStream.java:101) at com.sun.imageio.plugins.gif.GIFImageReader.getCode(GIFImageReader.java:306) at com.sun.imageio.plugins.gif.GIFImageReader.read(GIFImageReader.java:881) at javax.imageio.ImageIO.read(ImageIO.java:1422) at javax.imageio.ImageIO.read(ImageIO.java:1282) at temp.BufferedImageTest.main(BufferedImageTest.java:18)
报错图片文件:
下载
------解决方案--------------------------------------------------------
完全不需要用if,直接用异常捕捉来处理不就好了?
try {
ImageIO.xxxx();
} catch (RuntimeException ex) {
// 直接无视,或者用continue去做下一个循环好了
}