-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRename.java
More file actions
55 lines (53 loc) · 1.93 KB
/
Copy pathRename.java
File metadata and controls
55 lines (53 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/** * 重命名规则类 * @author jack */
class ReplacementChain{
private Map<String,String> map = new HashMap<String, String>();
public Map<String, String> getMap() {
return map;
}
// 添加新的替换规则(字符串替换)
public ReplacementChain addRegulation(String oldStr , String newStr){
this.map.put(oldStr, newStr);
return this;
}
}
/** * 重命名类 * @author Jack */
public class Rename {
/** * 批量重命名 *
* @param path *
* @param replacementChain */
public static void multiRename(String path,ReplacementChain replacementChain)
{
File file = new File(path);
boolean isDirectory = file.isDirectory();
/** 如果不是文件夹,就返回* */
if(!isDirectory){
System.out.println(path + "不是一个文件夹!");
return;
}
String[] files = file.list();
File f = null;
String filename = null;
String oldFileName = null; //之前的名字
/** 循环遍历所有文件* */
for(String fileName : files){
oldFileName = fileName; //記住原始的文件名
Map<String, String> map = replacementChain.getMap();
for (Map.Entry<String, String> entry : map.entrySet()) { //用for循环来替换文件名中多个不连续的字符串
fileName = fileName.replace(entry.getKey(), entry.getValue());
}
f = new File(path + "\\" + oldFileName); //输出地址和原路径保持一致
f.renameTo(new File(path + "\\" + fileName)); //renameTo()方法用於替換文件名
}
System.out.println("恭喜,批量重命名成功!");
}
public static void main(String[] args) {
ReplacementChain replacementChain = new ReplacementChain();
replacementChain.addRegulation("11957杨毅《全国计算机等级》四级操作系统原理", "计算机四级操作系统原理");//將文件中"测试"用""代替
// replacementChain.addRegulation(".720p.chs-eng.luckydag", "");//將文件中"再次测试"用""代替
multiRename("F:\\BaiduYunDownload\\计算机四级网络工程师\\操作系统\\讲解视频", replacementChain);
}
}