import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class BatchRename {
public static void main(String[] args) {
String dir = "c:/jsp/";
File file = new File(dir);
String srcSuffix = "jsp";
String dstSuffix = "ftl";
List<String> paths = listPath(file, srcSuffix);
for (String path : paths) {
File srcFile = new File(path);
String name = srcFile.getName();
int idx = name.lastIndexOf(".");
String prefix = name.substring(0, idx);
File dstFile = new File(srcFile.getParent() + "/" + prefix + "."
+ dstSuffix);
if (dstFile.exists()) {// 此处代码注意修改为自己想要的逻辑
srcFile.delete();
continue;
}
srcFile.renameTo(dstFile);
}
}
/**
* 获取指定路径下的所有符合条件的路径
*/
private static List<String> listPath(File path, String acceptSuffix) {
List<String> list = new ArrayList<String>();
File[] files = path.listFiles();
Arrays.sort(files);
for (File file : files) {
if (file.isDirectory()) {
List<String> _list = listPath(file, acceptSuffix);
list.addAll(_list);
} else {
String name = file.getName();
int idx = name.lastIndexOf(".");
String suffix = name.substring(idx + 1);
if (suffix.equals(acceptSuffix)) {
list.add(file.getAbsolutePath());
}
}
}
return list;
}
}
最近下载更多
nwj1225 LV11
2021年4月27日
Hubery_fight LV1
2018年12月25日
学习使我快乐ha LV1
2018年12月17日
杜若cccc LV1
2018年10月12日
车尔尼小鹿鹿 LV12
2018年8月25日
a1060355600 LV5
2018年8月16日
lineage3337 LV1
2018年1月24日
sunzihu123 LV4
2017年8月18日
wcyf0729 LV1
2017年7月18日
lzh0715 LV1
2017年2月16日

最近浏览
