您的位置:首页 > 其它

使用juniversalchardet-1.0.3.jar判断字符或文件的编码格式

2015-02-10 15:44 363 查看
参数:byte[]数组,字符串的话可以通过String 的getBytes()方法获得

public static String guessEncoding(byte[] bytes) {

String DEFAULT_ENCODING = "UTF-8";

org.mozilla.universalchardet.UniversalDetector detector =

new org.mozilla.universalchardet.UniversalDetector(null);

detector.handleData(bytes, 0, bytes.length);

detector.dataEnd();

String encoding = detector.getDetectedCharset();

detector.reset();

if (encoding == null) {

encoding = DEFAULT_ENCODING;

}

return encoding;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: