识别字符串的编码集,是一个让人很头痛的问题.
/**
* <p>
* 本类用来探测字符的编码集,关返回其名称
* </p>
*
* @author 惠万鹏
* @vision 1.08.08.06
*/
public class Detector
{
private static CodepageDetectorProxy detectorProxy;
static
{
detectorProxy = CodepageDetectorProxy.getInstance();
detectorProxy.add(JChardetFacade.getInstance());
}
public static synchronized String getEncodingType(String content)
throws IllegalArgumentException, IOException
{
ByteArrayInputStream stream = new ByteArrayInputStream(content
.getBytes());
return Detector.getEncodingType(stream, content.length());
}
public static synchronized String getEncodingType(File file)
throws MalformedURLException, IOException
{
return detectorProxy.detectCodepage(file.toURL()).name();
}
public static synchronized String getEncodingType(InputStream inputStream,
int length) throws IllegalArgumentException, IOException
{
return detectorProxy.detectCodepage(inputStream, length).name();
}
}
如果朋友们有更好的方法,请赐教.^0^