java实现批量生成文件夹和批量修改文件名

时间:2021-07-12 12:27:30

1、上代码:

package com.yanhui.util;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

@SuppressWarnings("rawtypes")
public class _fileUtil {
private Map map;
public _fileUtil(){
this.map=new HashMap();
}
public Map getMap() {
return map;
}

@SuppressWarnings("unchecked")
public _fileUtil addRegulation(String oldStr, String newStr){
this.map.put(oldStr, newStr);
return this;
}

public static void multiFloder(String path, String[] files, String suffixName){
String filename = "";
for(int i = 0; i<files.length;i++){
filename = (i+1)+"-"+files[i];
File f = new File(path+"\\"+filename+suffixName);
f.mkdirs();
System.out.println("创建文件夹成功!");
}
}

public static void multiRename(String path,_fileUtil 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 = "";
String oldFileName = "";

for(int i = 0; i<files.length;i++){
oldFileName = files[i];
filename = files[i];
Map map = replacementChain.getMap();
Iterator it = map.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry = (Entry) it.next();
filename = filename.replace((String)entry.getKey(), (String)entry.getValue());
}
filename = (i+1)+"-"+filename;
f = new File(path+"\\"+oldFileName);
f.renameTo(new File(path+"\\"+filename));
System.out.println("批量修改"+filename+"成功!");
}
}
}

2、生成指定的文件夹

String path = "F:\\test";
String[] files = {"a","b","c"};
String suffixName = "(艳辉网)";
_fileUtil.multiFloder(path, files , suffixName );

java实现批量生成文件夹和批量修改文件名

4、批量改文件的名称
原图:
java实现批量生成文件夹和批量修改文件名
修改后:
java实现批量生成文件夹和批量修改文件名
代码实现:

_fileUtil replacementChain = new _fileUtil();
replacementChain.addRegulation(".mp4", "(艳辉网分享福利的网站).mp4");
_fileUtil.multiRename(false,"F:\\test\\1-a(艳辉网)", replacementChain);

全CSDN的丰(好)色(se)博客,这里有书本看不到的Java技术,电影院禁播的电影,欢迎关注QQ群494808400