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();
}
文档
Views: 2,626 · Posted: 2020-09-25
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Loading...