当前位置: 首页 > 工具软件 > 重名修改 > 使用案例 >

Java防重名

孙经艺
2023-12-01
 /**
     * 批量下载防重名
     *
     * @param fileNames         文件名列表
     * @param substringFileName 需防重名文件名
     * @param index             默认0
     * @return
     */
    public String getFileName(List<String> fileNames, String substringFileName, Integer index) {
        if (fileNames.size() < 1) {
            return substringFileName;
        }
        Boolean flag = false;
        for (String fileName : fileNames) {
            if (fileName.equals(substringFileName)) {
                flag = true;
                break;
            }
        }
        if (flag) {
            //重命名
            Integer i = substringFileName.lastIndexOf(".");
            String flagIndex = "(" + index + ")";
            if (i > 0) {
                //1.有.
                String start = substringFileName.substring(0, i);
                if (start.contains((flagIndex))) {
                    start = start.replace(flagIndex, "");
                }
                index++;
                start = start + "(" + index + ")";
                String end = substringFileName.substring(i);
                substringFileName = start + end;
            } else {
                //2.无.
                if (substringFileName.contains((flagIndex))) {
                    substringFileName = substringFileName.replace(flagIndex, "");
                }
                index++;
                substringFileName = substringFileName + "(" + index + ")";
            }
            substringFileName = this.getFileName(fileNames, substringFileName, index);

        }
        return substringFileName;
    }
 类似资料: