批量修改文件名

public static void changeFileName(String path){

File file = new File(path);

if(file.exists()){

File[] files = file.listFiles();

if (null == files || files.length == 0) {

System.out.println("文件夹是空的!");

return;

} else {

for (File file2 : files) {

if (file2.isDirectory()) {

changeFileName(file2.getAbsolutePath());

} else {

System.out.println("文件:" + file2.getAbsolutePath());

String filePath = file2.getAbsolutePath();

String fileName = filePath.substring(0,filePath.lastIndexOf("\\"))+"\\1"+file2.getName();

File oriFile = new File(filePath);

System.out.println("新文件名:" + fileName);

boolean b = oriFile.renameTo(new File(fileName));

System.out.println(b);

}

}

}

}else{

System.out.println("该路径不存在");

}

}

你可能感兴趣的:(File,java)