Java实现批量去除文件夹名称中指定的字符
作者:互联网
import java.io.File;
public class UpdateName {
//AD为广告内容
public static final String AD = "[www.domp4.com]";
public static int fileNum = 0;
public static void main(String[] args) {
//文件夹路径名
String rootPath = "C:/360极速浏览器下载/巡回检查组";
scanFile(rootPath);
System.out.println("共去广告" + fileNum + "个文件");
}
/*
* 递归调用查找指定文件夹下所有文件
*/
public static void scanFile(String path) {
File dirFile = reName(new File(path));
System.out.println(dirFile.getAbsolutePath());
if (dirFile.isDirectory()){
String[] fileList = dirFile.list();
for (int i = 0; i < fileList.length; i++) {
path = dirFile.getAbsolutePath() + "\\" + fileList[i];
scanFile(path);
}
}
}
public static File reName(File oldFile) {
//不带路径的文件名
String originalName = oldFile.getName();
if (originalName.contains(AD)) {
//带路径的文件名
String oldFilePath = oldFile.getAbsolutePath();// 目录路径
String newFilePath = oldFilePath.replace(AD, "");
File newFile = new File(newFilePath);
if (oldFile.renameTo(newFile)) {
fileNum++;
return newFile;
}
}
return oldFile;
}
}
标签:Java,String,dirFile,文件夹,static,File,去除,oldFile,public 来源: https://blog.csdn.net/weixin_43552513/article/details/117380661