前段时间遇到个批量修改文件名的问题,做下笔记。
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));
}
}
}
}
}
修改前:

修改后:


本文介绍了一种使用Java进行批量文件重命名的方法,通过递归遍历指定目录下的所有文件,将符合特定条件的文件名更改为新的名称。该方法适用于需要大规模修改文件名的场景。
640

被折叠的 条评论
为什么被折叠?



