識別字符串的編碼集,是一個讓人很頭痛的問題.
/**
* <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^