问题描述:命名出来的都是四位数例如0001,0002,0003后面+.txt或者.jpg之类的
import java.io.File;
/**
* @author tky
* @date 2019-05-07 22:59
* 批量修改图片名字
*/
public class ImageRename {
public static void main(String[] args) {
//fs为存储文件的字符数组
String path = "H:/biZhi/picture";
File file = new File(path);
String[] fs = file.list();
//定义循环中需要的变量
String newName = "";
String oldName = "";
File f = null;
int index = 0;
int len = ("" + fs.length).length();
for (int i = 0; i < fs.length; i++) {
oldName = fs[i];
index = oldName.lastIndexOf(".");
//制造0001、0002数字
newName = String.format("%0" + len + "d", i + 1);
//截取oldName中.txt
newName += oldName.substring(index, oldName.length());
f = new File(path + "/" + oldName);
f.renameTo(new File(path + "/" + newName));
}
System.out.println("运行结束");
}
}