栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;


public class RenameFileUtil {
    //私有构造器
    private RenameFileUtil() {
    }

    
    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.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/978654.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号