java实现 批量转换文件编码格式

java实现 批量转换文件编码格式
转载自https://www.cnblogs.com/enenen/p/10761362.html
在原有基础上增加了原始编码识别跳过已经是目标编码格式的文件。只需要传文件夹的绝对路径,目标编码格式,需要转换的文件扩展名三个参数就可以了

需要的依赖

<dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.5.6</version>
        </dependency>
        <dependency>
            <groupId>com.googlecode.juniversalchardet</groupId>
            <artifactId>juniversalchardet</artifactId>
            <version>1.0.3</version>
        </dependency>
public class ConcertEncodeing {

    public static void main(String[] args) {
        
        convertCharset("E:\\编程学习\\it-study\\其他\\javaWeb-demo - 副本",StandardCharsets.UTF_8,"java");
        
    }
    
    /**
     * 转换文件编码格式
     * @param path 需要转换的文件或文件夹路径
     * @param toCharset   目标编码格式
     * @param expansion      需要转换的文件扩展名,如需全部转换则传 null
     */
    private static void convertCharset(String path,Charset toCharset,String expansion ){
        if (StrUtil.isBlank(path)) {
            return;
        }
        File file = FileUtil.file(path);
        File[] listFiles = file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                    if ((FileUtil.isDirectory(pathname)) || StrUtil.isBlank(expansion) || (!StrUtil.isBlank(expansion) && (FileUtil.extName(pathname).equals("java")))){
                        return true;
                    }
                return false;
            }
        });
        for (int i = 0; i < listFiles.length; i++) {
            if (listFiles[i].isDirectory()) {
                String canonicalPath = FileUtil.getCanonicalPath(listFiles[i]);
                //每个文件夹分个线程处理,提高点儿效率
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        convertCharset(canonicalPath,toCharset,expansion);
                    }
                }).start();
            }else {
                try {
                    Charset fromCharset = Charset.forName(getFileEncode(listFiles[i]));
                    if(!fromCharset.equals(toCharset)) {
                        FileUtil.convertCharset(listFiles[i], fromCharset, toCharset);
                        Console.log("转换完成文件名:{}", listFiles[i].getName());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 获得文件编码格式
     *
     * @param file
     * @return
     * @throws IOException
     */
    public static String getFileEncode(File file) throws IOException {
        FileInputStream in = new FileInputStream(file);
        String code = "utf-8";
        byte[] buf = new byte[4096];
        UniversalDetector detector = new UniversalDetector(null);
        int nread;
        while ((nread = in.read(buf)) > 0 && !detector.isDone()) {
            detector.handleData(buf, 0, nread);
        }
        detector.dataEnd();
        String encoding = detector.getDetectedCharset();
        if (encoding!= null && !encoding.trim().equals("")) {
            code = encoding;
        }
        return code;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值