20220616 如何封装File类进行批量文件重复改名

It is no exaggeration to say that Java is one of the best language in the world rather than PHP~.

I got some study resources from a Java Wechat group about Tanhua series of ITHEIMA.The content is closed to porn topic to attract students’ interests,yeah,that is funny.

First and foremost,I directly downloaded it from Bilibili.com to avoid they may be reported by someone in the future.

Here it is the problem I met. The length of name of the videos I got from Bili Helper is too long to read.Thereby, I need to try some methods to deal with a batch of them.

在这里插入图片描述

That’s why I would like to use File.Class to rename.

let’s go straight to the codes
@Test
    void test22222() {
        File f = new File("D:\\v");
        File fs[] = f.listFiles();
        for (int i = 0; i < fs.length; i++) {
            String name = fs[i].getName();
            String replace=name.replace("探花交友_精通Spring_Cloud_Alibaba_Dubbo一栈式微服务架构,即时通讯、人脸识别等五大解决方案全掌握," +
                    "工作时拿来即用_黑马最新高级课程.", "");
            File newFile = new File("D:\\v\\" + replace);
            fs[i].renameTo(newFile);
        }
    }
import java.io.File;

/**
 * @Description:${<VARIABLE_NAME>}
 * @Author:Spike Wong
 * @Date:2022/6/16
 */
public class RenameFileUtil {
    //私有构造器
    private RenameFileUtil() {
    }

    /**
     * 更改文件的重复信息
     *
     * @param filePath
     * @param target
     * @param replacement
     */
    public static void replaceName(String filePath, String target, String replacement) {
        File f = new File(filePath);
        File fs[] = f.listFiles();
        for (int i = 0; i < fs.length; i++) {
            String name = fs[i].getName();
            String replace = name.replace(target, replacement);
            File newFile = new File(filePath + File.separator + replace);
            fs[i].renameTo(newFile);
        }
    }
}
Here is the effect of the util.

是

Ok, see you next time~ I will miss you dear.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值