用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
);
}
}
}
运行后的效果