Java 中识别字符串编码

Java About 805 words

添加依赖

<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

Views: 2,286 · Posted: 2020-09-25

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

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

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


Today On History
Browsing Refresh