检查字符串是什么编码字符集
引入依赖
<dependency>
<groupId>com.googlecode.juniversalchardet</groupId>
<artifactId>juniversalchardet</artifactId>
<version>1.0.3</version>
</dependency>
包装一下
public class CharsetUtil {
/**
* 获取字符(串字节数组格式)的字符集
*
* @param bytes 字符串的字节数组
* @return 字符集
*/
public static String getCharset(byte[] bytes) {
String defaultCharset = "UTF-8";
UniversalDetector detector = new UniversalDetector(null);
detector.handleData(bytes, 0, bytes.length);
detector.dataEnd();
detector.reset();
String detectedCharset = detector.getDetectedCharset();
return detectedCharset == null ? defaultCharset : detectedCharset;
}
}
验证
@Test
void getCharset() {
String hello = "hello, world";
System.out.println("hello charset: " + CharsetUtil.getCharset(hello.getBytes()));
}
输出(与项目字符集设置有关):
hello charset: UTF-8
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步