参数: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;
}
本文介绍了一个方法,通过使用UniversalDetector类,自动检测给定字节数组的编码方式,若未检测到,则默认为UTF-8。
448

被折叠的 条评论
为什么被折叠?



