package com.ktnet.tradesign;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ibm.icu.text.CharsetDetector;
import com.ibm.icu.text.CharsetMatch;
public class CharDetector {
private String[] data = {
"ms949.txt",
"utf8.txt"
};
Logger logger = LoggerFactory.getLogger(CharDetector.class);
@Test
public void detect() throws IOException {
CharsetDetector detector;
CharsetMatch match;
FileInputStream fis = null;
try {
for(String fn : data) {
File f = new File(fn);
fis = new FileInputStream(f);
byte[] byteData = new byte[(int) f.length()];
fis.read(byteData);
fis.close();
detector = new CharsetDetector();
detector.setText(byteData);
match = detector.detect();
logger.info(fn + " encoding is \"" + match.getName() + "\"");
}
}
finally {
IOUtils.closeQuietly(fis);
}
}
}
JAVA