重命名某路径下的文件名为另外一个

package org.example.testMain1;

import java.io.File;
import java.nio.file.Path;

public class Main1 {
    public static void main(String[] args) throws Exception {
        String path = "E:\\03_my_git\\train";

        File file = new File(path);

        process(file);
    }

    public static void process(File targetFile) throws Exception {
        if (targetFile == null || !targetFile.exists()) {
            return;
        }
        for (File file : targetFile.listFiles()) {
            if (file.isDirectory()) {
                process(file);
            } else {
                String oldFileName = file.getName();
                String newFileName = oldFileName.replace("(平价资源站666 root.com)", "");

                if (!oldFileName.equals(newFileName)) {
                    String newPath = Path.of(file.getParentFile().getAbsolutePath(), newFileName).toString();

                    System.out.println(file.getAbsolutePath() + "-->" + newPath);
                    file.renameTo(new File(newPath));
                }
            }
        }
    }
}

你可能感兴趣的:(#,java,java)