將可能出現重複的一堆文件名重命名

   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;
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章