将以前的老代码,批量转换为UTF-8编码(可指定)

本文介绍了一个用于批量转换文件编码的Java程序。该程序能够将指定目录下的多种类型的源文件从一种字符集编码转换为另一种编码,如从GBK转换到UTF-8。支持的文件类型包括.java等常见源代码文件,并提供了命令行参数来指定源目录及编码。
import java.io.*;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;

public class Main {
    private static List<String> reencodingFileSuffix = Arrays.asList(
            ".java"
//            ".sql",
//            ".txt",
//            ".config",
//            ".conf",
//            ".properties",
//            ".xml",
//            ".css",
//            ".html",
//            ".htm",
//            ".jsp"
    );
    public static void main(String[] args) throws IOException {
        //init
        if (args.length == 0) {
            throw new IllegalArgumentException("请输入项目完整路径");
        }
        String basePath = args[0];
        String srcEncoding = "GBK";
        String toEncoding = "UTF-8";
        System.out.println("待转换项目路径:" + basePath);
        System.out.println("转换后项目路径:" + basePath + "_reencoding");
        if (args.length == 3) {
            srcEncoding = args[1];
            toEncoding = args[2];
        }
        System.out.println("项目由 " + srcEncoding + " 转换至 " + toEncoding);

        // path check
        File srcPathFile = new File(basePath);
        if (!srcPathFile.exists() || !srcPathFile.isDirectory()) {
            System.out.println("不存在的路径/不是目录,拒绝转换");
            return;
        }
        File destPathFile = new File(srcPathFile.getAbsolutePath() + "_reencoding");
        if (destPathFile.exists()) {
            throw new IllegalStateException(destPathFile.getAbsolutePath() + "已经存在");
        }

        //reencoding
        reencoding(srcPathFile, destPathFile, srcEncoding, toEncoding);
    }
    private static void reencoding(File baseSrcFolder, File baseDestFolder, String srcEncoding, String toEncoding) throws IOException {
        if (!baseDestFolder.exists()) {
            baseDestFolder.mkdir();
        }
        File[] srcFiles = baseSrcFolder.listFiles();
        for (File srcFile : srcFiles) {
            File destFilePath = new File(baseDestFolder.getAbsolutePath() + "/" + srcFile.getName());
            if (srcFile.isDirectory()) {
                reencoding(srcFile, destFilePath, srcEncoding, toEncoding);
            } else {
                int dotLastIndex = srcFile.getName().lastIndexOf('.');
                String suffix = dotLastIndex == -1 ? null : srcFile.getName().substring(dotLastIndex);
                if (dotLastIndex == -1 || !reencodingFileSuffix.contains(suffix)) {
                    Files.copy(srcFile.toPath(), destFilePath.toPath());
                    System.out.println("原样拷贝 " + srcFile.getAbsolutePath() + " 完成");
                } else {
                    try (
                            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile), srcEncoding));
                            PrintWriter out = new PrintWriter(destFilePath.getAbsolutePath(), toEncoding)
                            ) {
                        String line = null;
                        while ((line = in.readLine()) != null) {
                            out.println(line);
                        }
                        System.out.println("拷贝并转换编码 " + srcFile.getAbsolutePath() + " 完成");
                    } catch (IOException e) {
                        throw e;
                    }
                }
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值