Java中识别字符串编码

Java大约 802 字

添加依赖

<dependency>
    <groupId>com.googlecode.juniversalchardet</groupId>
    <artifactId>juniversalchardet</artifactId>
    <version>1.0.3</version>
</dependency>

代码

public static void main(String[] args) {
//  byte[] body = "汉字".getBytes(StandardCharsets.UTF_8);
    byte[] body = "汉字".getBytes(Charset.forName("GB2312"));
    UniversalDetector detector = new UniversalDetector(null);
    detector.handleData(body, 0, body.length);
    detector.dataEnd();

    String encoding = detector.getDetectedCharset();
    if (encoding != null) {
        System.out.println("Detected encoding = " + encoding);
    } else {
        System.out.println("No encoding detected.");
    }

    detector.reset();
}

文档

https://code.google.com/archive/p/juniversalchardet

阅读 65 · 发布于 2020-09-25

————        END        ————

扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看换一批