前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >批量去除文件,文件夹的名称中指定的字符。

批量去除文件,文件夹的名称中指定的字符。

作者头像
崔笑颜
发布2020-06-08 16:33:02
1.3K0
发布2020-06-08 16:33:02
举报
文章被收录于专栏:小小码农一个。

效果如下

执行前

20191029095253196
20191029095253196

执行后

20191029095319557
20191029095319557

完整代码

代码语言:javascript
复制
package com.we;
import java.io.File;

/**
 * 批量去除文件、文件夹的名称中指定的字符
 * @author WalterWen
 *
 */
public class ClearAdvert {
	//AD为广告内容
    public static final String AD = "要去除的字符";
    public static int fileNum = 0;
 
    public static void main(String[] args) {
        //文件夹路径名
        String rootPath = "指定的文件(夹)路径";
        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;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-10-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 执行前
  • 执行后
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档