1、递归文件遍历,整理以遍历文件夹在的所有符合的文件并更改后缀名为例
private static int cnt = 0;
/**
* 批量重命名
* @param path 文件夹路径
* @param srcSuffs 需要修改的后缀
* @param modSuff 需要改成的后缀
*/
public static void modFilesSuffix(String path,
List<String> srcSuffs, String modSuff) {
if ((srcSuffs == null || srcSuffs.size() <= 0)
|| (modSuff == null || modSuff.equals(""))) {
System.out.println("modFilesSuffix 参数错误");
return;
}
File file = new File(path);
if (file.exists()) {// 如果文件存在
File[] files = file.listFiles();
if (files.length == 0) {// 如果文件夹是空的
System.err.println("文件夹是空的");
return;
} else {//
for (File f : files) {
if (f.isDirectory()) {// 如果是目录,递归
modFilesSuffix(f.getAbsolutePath(),
srcSuffs, modSuff);
} else {
String fname = f.getName();
int pos = fname.lastIndexOf(".");
String prefix = fname.substring(pos + 1);
if (srcSuffs.contains(prefix)) {
f.renameTo(new File(path + "\\" +
fname.substring(0, pos + 1) + modSuff));
cnt++;
}
}
}
}
} else {
System.err.println("文件夹不存在");
}
System.out.println("一共修改了 " + cnt + "个文件");
}