package fileTest;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Spliterator;
import javax.imageio.ImageIO;
public class FileName {
public static void main(String[] args) throws IOException {
String dirName="C:\\Users\\dsp\\Desktop\\JP首页图片";//存放图片的文件夹
File f=new File(dirName);
ArrayList<String> path=new ArrayList<String>();//存放每个图片的路径
if (f.isDirectory()) {
String[] imgs=f.list();
for(int i=0;i<imgs.length;i++) {
System.out.println(imgs[i]);
String name = dirName+"\\"+imgs[i];
path.add(name);
}
}
for (String object : path) {
System.out.println(object);
String[] nf=object.split("\\.");//通过split截取文件路径
System.out.println(nf[0]);
File f2=new File(object);
BufferedImage srcImg = ImageIO.read(f2);//使用imgeIO来读取图片
ImageIO.write(srcImg, "png", new File(nf[0]+".png"));//重新创建图片
}
}
}
原文地址:http://blog.51cto.com/12390959/2092997