Java 批量删除文件名中指定字符串并更改视频后缀名
以下是 Java 实现:
import java.io.File;
public class ReplaceFiles {
public static void main(String[] args) {
// 输入根目录
String rootPath = "/path/to/root";
File rootDir = new File(rootPath);
// 递归遍历目录
replaceFiles(rootDir);
}
private static void replaceFiles(File dir) {
// 遍历目录中的文件和子目录
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
// 如果是子目录,递归处理
replaceFiles(file);
} else {
// 如果是文件,进行替换操作
String fileName = file.getName();
if (fileName.contains('【666资源网:666java.com】')) {
// 如果文件名包含指定字符串,进行替换操作
String newFileName = fileName.replace('【666资源网:666java.com】', "");
if (newFileName.endsWith(".m2ts")) {
// 如果是视频文件,将后缀名改为 .mp4
newFileName = newFileName.replace(".m2ts", ".mp4");
}
File newFile = new File(file.getParent(), newFileName);
file.renameTo(newFile);
}
}
}
}
}
该程序会递归遍历输入的根目录,并将所有文件名中包含指定字符串的文件名替换为空字符串。如果文件名以 .m2ts 结尾,则将后缀名改为 .mp4。注意,该程序没有对文件内容进行修改,仅修改文件名。
原文地址: http://www.cveoy.top/t/topic/nlP2 著作权归作者所有。请勿转载和采集!