import java.io.File; public class FileRename { public static void main(String[] args) { String dir = "F:\JAVA\hibernate\"; //选择要批量更改文件的文件夹,记得后面要加上\表示文件夹下面的文件 File file = new File(dir); String fileName[] = file.list(); int number = fileName.length; //获取文件数量 File newFile[] = new File[number]; System.out.println("number = " + number); for(int i=0; i<number; i++) { System.out.println("第"+(i+1)+"个文件名" + ":" + fileName[i]); //查看所有文件 newFile[i] = new File(dir+fileName[i]); } System.out.println("============分割线============="); for(int i=0; i<number;i++){ String fn = fileName[i]; String newFileName = fn.substring(0, 3)+fn.substring(22); //根据原来文件名来更改,用substring(begin,end)来截取字符串 System.out.println(newFileName); boolean flag = newFile[number - (i+1)].renameTo(new File(dir + newFileName+".avi")); if(flag){ System.out.println("重命名成功!"+":"+newFileName); System.out.println("重命名成功"+(i+1)); } else { System.out.println("失败"); } } } }