首页 > 其他分享 >将可能出现重复的一堆文件名重命名

将可能出现重复的一堆文件名重命名

时间:2023-03-20 15:55:33浏览次数:29  
标签:重命名 一堆 name 文件名 List newFileNameList value entry String

   private static List<String> getNoRepeatFileNameList(List<String> fileNameList) {
        Map<String, List<String>> fileNameMap = fileNameList.stream().collect(Collectors.groupingBy(x -> x));
        List<String> newFileNameList = new ArrayList<>();
        for (Map.Entry<String, List<String>> entry : fileNameMap.entrySet()) {
            String key = entry.getKey();
            List<String> value = entry.getValue();
            if (value.size() > 1) {
                int index = 1;
                for (String name : value) {
                    String newName = String.format("%s(%s)%s", name.substring(0, name.lastIndexOf(".")), (index++), name.substring(name.lastIndexOf(".")));
                    newFileNameList.add(newName);
                }
            } else {
                newFileNameList.add(key);
            }
        }
        return newFileNameList;
    }

 

标签:重命名,一堆,name,文件名,List,newFileNameList,value,entry,String
From: https://www.cnblogs.com/hdwang/p/17236596.html

相关文章