java实现批量修改文件夹名字以及修改文件名使用IO文件流(若依二开适用)

使用场景:

        在批量更改完毕类名以及启动类的名字之后发现出现二级文件的情况如下图,这个时候是因为将内容中的启动类名修改但是没有修改文件名字导致的。

提供代码进行文件夹以及文件夹中所有的子文件包含文件夹中的名字批量修改替换

import java.io.File;

public class RenameFiles {

    public static void main(String[] args) {
        String directoryPath = "E:\\desktop\\YunSeven-Cloud";
        renameFiles(directoryPath);
    }

    public static void renameFiles(String directoryPath) {
        File directory = new File(directoryPath);
        File[] filesList = directory.listFiles();

        if (filesList != null) {
            for (File file : filesList) {
                if (file.isFile()) {
                    // 重命名文件的逻辑
                    String fileName = file.getName();
                    String newName = fileName.replaceAll("ruoyi", "yunseven").replaceAll("需要修改的名字(可以是包含)","修改过后的名字");
                    File newFile = new File(file.getParent() + "\\" + newName);

                    if (file.renameTo(newFile)) {
                        System.out.println("File " + fileName + " renamed to " + newName);
                    } else {
                        System.out.println("Could not rename file " + fileName);
                    }
                } else if (file.isDirectory()) {
                    // 重命名文件夹的逻辑
                    String directoryName = file.getName();
                    String newDirectoryName = directoryName.replaceAll("ruoyi", "yunseven");
                    File newDirectory = new File(file.getParent() + "\\" + newDirectoryName);

                    if (file.renameTo(newDirectory)) {
                        System.out.println("Directory " + directoryName + " renamed to " + newDirectoryName);
                    } else {
                        System.out.println("Could not rename directory " + directoryName);
                    }

                    // 递归调用以处理文件夹中的子文件和子文件夹
                    renameFiles(newDirectory.getAbsolutePath());
                }
            }
        }
    }
}

 使用说明:

        使用递归来进行文件的判断(原理可参考遍历数),递归判断文件名字前缀是否为需要替换的字符串,如果是则将其替换掉。

        中间的中文字符按照需求进行修改即可

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值