java将一个文件夹里目录文件名的中文一二三替换成数字123等

/**
     * ########################################################################################
     * 将文件或文件夹中的一二三等全部替换为123
     *
     * @param pathName 路径
     */
    public static void replace123(String pathName) {
        logger.info("{}", pathName);
        File file = new File(pathName);
        File newFile = newFilePath(file);
        /* 1,获取该文件夹下的所有的文件和文件夹 */
        File[] subFiles = newFile.listFiles();
        /* 2,遍历数组 */
        if (subFiles.length != 0) {
            for (File subFile : subFiles) {
                if (subFile.isFile()) {
                    //如果是文件就重命名
                    newFilePath(subFile);
                } else {
                    //如果是文件夹就循环调用
                    replace123(subFile.getPath());
                }
            }
        }
    }

    /**
     * 对文件重命名
     *
     * @param subFile
     */
    private static File newFilePath(File subFile) {
        String path00String = subFile.getName();
        path00String = path00String.replace("一", "1").replace("二", "2").replace("三", "3")
                .replace("四", "4").replace("五", "5").replace("六", "6")
                .replace("七", "7").replace("八", "8").replace("九", "9");
        boolean isTen = path00String.contains("十");
        if (isTen) {
            int asd = path00String.indexOf("十");
            int lastIndex = path00String.length() - 1;
            //是否第一个字符
            boolean first = false;
            if (asd == 0) {
                first = true;
            }
            //是否最后一个字符
            boolean last = false;
            if (asd == lastIndex) {
                last = true;
            }
            if (first && last) {
                //左右都没有
                path00String = path00String.replace("十", "10");
            } else if (first && !last) {
                //左没有右有
                char lastP = path00String.charAt(asd + 1);
                if (isNumber(lastP + "")) {
                    path00String = path00String.replace("十", "1");
                } else {
                    path00String = path00String.replace("十", "10");
                }
            } else if (!first && last) {
                //左有右没有
                char firstP = path00String.charAt(asd - 1);
                if (isNumber(firstP + "")) {
                    path00String = path00String.replace("十", "0");
                } else {
                    path00String = path00String.replace("十", "10");
                }
            } else if (!first && !last) {
                //左右都有
                char firstP = path00String.charAt(asd - 1);
                char lastP = path00String.charAt(asd + 1);
                if (isNumber(firstP + "") && isNumber(lastP + "")) {
                    path00String = path00String.replace("十", "");
                } else if (!isNumber(firstP + "") && isNumber(lastP + "")) {
                    path00String = path00String.replace("十", "1");
                } else if (isNumber(firstP + "") && !isNumber(lastP + "")) {
                    path00String = path00String.replace("十", "0");
                } else if (!isNumber(firstP + "") && !isNumber(lastP + "")) {
                    path00String = path00String.replace("十", "10");
                }

            }

        }
        File newFile = new File(subFile.getParent() + "\\" + path00String);
        subFile.renameTo(newFile);
        return newFile;

    }

    /**
     * 是数字
     *
     * @param str
     * @return
     */
    public static boolean isNumber(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (!(str.charAt(i) >= '0' && str.charAt(i) <= '9')) {
                return false;
            }
        }
        return true;
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容