前段时间遇到个批量修改文件名的问题,做下笔记。
Demo:
/**
* 2019年9月10日下午5:11:57
*/
package testRenameFiles;
import java.io.File;
/**
* @author XWF
*
*/
public class TestRenameFiles {
private static String path = "D:/Temp";
private static String oldName = "test.txt";
private static String newName = "new.txt";
public static void main(String[] args) {
File file = new File(path);
renameFile(file);
System.out.println("finished.");
}
private static void renameFile(File file) {
if(file != null) {
if(file.isDirectory()) {
File[] chfs = file.listFiles();
for (File chf : chfs) {
renameFile(chf);
}
}else {
if(oldName.equals(file.getName())) {
System.out.println("start rename:" + file.getAbsolutePath());
file.renameTo(new File(file.getParent() + "/" + newName));
}
}
}
}
}
修改前:
修改后: