文件重命名工具

文件重命名工具

你是否还在为文件批量重命名发愁,下面的代码很好的解决了文件重命名问题

实现代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
String[] args = new String[]{"C:\\tests"};
String matchRegex = "(\\d{6})";
String replaceRegex = "$1.json";

Pattern pattern = Pattern.compile(matchRegex);

File file = new File(args[0]);
if (file.exists()) {
File[] files = file.listFiles();
if (files != null) {
System.out.println("files : " + files.length);
for (File f : files) {
Matcher matcher = pattern.matcher(f.getName());
if (matcher.matches()) {
String s = matcher.replaceAll(replaceRegex);
System.out.println(s);
f.renameTo(new File(f.getParent() + File.separator + s));
}
}
}
}