用Java批量去除文件中一些广告名
网页或者淘宝下载一些东西时,文件名会被批量加一些广告如下所示,今天给大家分享一个批量去除这些广告的小程序,直接上代码了。
处理前
代码
package net.dxclass.demo.controller;
/* *
* 自动扫描文件夹下的所有文件并去除文件名中的广告字段
* */
import java.io.File;
public class RemoveRedundancy {
//要删除的内容
public static final String add = "[123123广告]";
public static void main(String[] args) {
//文件所在目录
String path = "H:\\BaiduNetdisDownload3\\ssm框架角色权限后台管理系统脚手架开发实战教程包含完整源码";
ScanFile(path);
}
/*
* 递归调用查找指定文件加下所有文件
*/
private static String ScanFile(String path) {
File rootDir = new File(path);
if (!rootDir.isDirectory()) {
//如果是根目录就去除,否则继续递归
reName(rootDir);
} else{
//获取该目录下的所有文件
String[] fileList = rootDir.list();
for (int i = 0; i < fileList.length; i++) {
path = rootDir.getAbsolutePath() + "\\" + fileList[i];
System.out.println(path);
ScanFile(path);
}
}
return null;
}
private static void reName(File f) {
String originalName = f.getName();
if (originalName.contains(add)){
String dirPath = f.getAbsolutePath();//得到文件名
String newString = dirPath.replace(add, "");
File finalName = new File(newString);
f.renameTo(finalName);
}
}
}