icu4j解决获取文件编码问题

记一次项目中遇到的坑。线上系统有解析CSV与txt文本文件的需求,文本文件包含中文,同时文件的字符编码无法统一,自己尝试写了一些获取文件字符编码的工具类,效果不是很理想。在网上查阅了很多资料,觉得icu4j比较符合我的业务,于是将icu4j引入到项目中使用。

  • 引入icu4j依赖
        
            com.ibm.icu
            icu4j
            70.1
        
  • 编写工具类
public class CharsetEncodingUtils {
    public static String getCharset(InputStream in) throws IOException {
        String charset = null;
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(in);
            CharsetDetector cd = new CharsetDetector();
            cd.setText(bis);
            CharsetMatch cm = cd.detect();
            if (cm != null) {
                charset = cm.getName();
            } else {
                throw new UnsupportedCharsetException("获取文件编码失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new IOException(e);
        }finally {
            if (null != bis) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != in) {
                in.close();
            }
        }

        return charset;
    }

    public static void main(String[] args) {
        File file = new File("/Users/xxxxx/Documents/批量添加验证样本模板111/上传文件样例-表格 1.txt");
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
            String charset = getCharset(inputStream);
            System.out.println("charset:" + charset);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
  • 测试case
    以下是我做的16个case,使用16种字符编码的文件,通过icu4j来获取文件的字符编码,目前测试来看只要源文件的中文能正常显示,使用icu4j读取到的编码就能正常解析,同时将解析到的内容以UTF-8的编码输出到新的文件,也没问题。
输入文件字符编码 ICU4J读取文件字符编码 输出UTF-8 备注
ANSI GB18030 中文转码通过
BOM UTF-8 UTF-8 中文转码通过
UTF-16BE UTF-16BE 中文转码通过
UTF-16BE with BOM UTF-16BE 中文转码通过
UTF-16LE UTF-16LE 中文转码通过
UTF-16LE with BOM UTF-16LE 中文转码通过
UTF-8 UTF-8 中文转码通过
UTF-7 ISO-8859-1 中文转码未通过 源文件中文已经乱码
UTF-32 UTF-32LE 中文转码通过
UTF-32BE UTF-32BE 中文转码通过
UTF-32LE UTF-32LE 中文转码通过
GB 18030 GB18030 中文转码通过
GBK GB18030 中文转码通过
ISO 2022-CN ISO-2022-CN 中文转码通过 源文件中文已经乱码
DOS Latin 2 ISO-8859-1 中文转码未通过 源文件中文已经乱码
ASCLL ISO-8859-1 中文转码未通过 源文件中文无法显示,被ASCLL编码

参考stackoverflow,原文中还介绍了很多种方式

你可能感兴趣的:(icu4j解决获取文件编码问题)