検出符号化ツールjunniversalchardet
1450 ワード
// TODO Auto-generated method stub
byte[] buf = c.crawl("http://sse.bupt.edu.cn/");
//String fileName = args[0];
// String fileName = "d:/test.txt";
// java.io.FileInputStream fis = null;
// try {
// fis = new java.io.FileInputStream(fileName);
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// (1)
UniversalDetector detector = new UniversalDetector(null);
// (2)
detector.handleData(buf, 0, buf.length);
// int nread;
// try {
// while ((nread = fis.read(buf)) > 0 && !detector.isDone()) {
// detector.handleData(buf, 0, nread);
// }
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// (3)
detector.dataEnd();
// (4)
String encoding = detector.getDetectedCharset();
if (encoding != null) {
System.out.println("Detected encoding = " + encoding);
} else {
System.out.println("No encoding detected.");
}
// (5)
detector.reset();