Java遍历一个文件夹 把文件夹里面的目录名称或者文件视频 名称 带有 尹志伟牛逼 文字的全部替换为空串
以下是Java代码示例:
import java.io.File;
public class ReplaceYinZhiwei {
public static void main(String[] args) {
String folderPath = "/path/to/folder"; // 文件夹路径
File folder = new File(folderPath);
replaceYinZhiwei(folder);
}
private static void replaceYinZhiwei(File file) {
if (file.isDirectory()) { // 如果是文件夹,递归遍历
File[] files = file.listFiles();
for (File subFile : files) {
replaceYinZhiwei(subFile);
}
} else { // 如果是文件,替换文件名
String fileName = file.getName();
if (fileName.contains("尹志伟牛逼")) {
String newFileName = fileName.replace("尹志伟牛逼", "");
File newFile = new File(file.getParentFile(), newFileName);
file.renameTo(newFile);
}
}
}
}
该代码会递归遍历指定的文件夹,如果遇到文件名或目录名包含"尹志伟牛逼"的文件或目录,就将其名称中的"尹志伟牛逼"替换为空串。注意,该代码中的文件名替换只会替换文件名,不会替换文件内容。
原文地址: http://www.cveoy.top/t/topic/b0Ex 著作权归作者所有。请勿转载和采集!