public static String detectEncoding(String filepath) throws IOException {
byte[] buf = new byte[4096];
FileInputStream fis = new FileInputStream(filepath);
// Construct an instance of UniversalDetector
UniversalDetector detector = new UniversalDetector(null);
// Feed some data to the detector
int nread;
while ((nread = fis.read(buf)) > 0 && !detector.isDone()) {
detector.handleData(buf, 0, nread);
}
// Notify the detector of the end of data
detector.dataEnd();
// Get the detected encoding name
String detectedEncoding = detector.getDetectedCharset();
if (detectedEncoding != null) {
LOG.info(String.format("The encoding of the file is %s", detectedEncoding));
} else {
LOG.warn(String.format("No encoding detected! Use defaul %s", Utils.encoding));
detectedEncoding = Utils.encoding;
}
// Reset the detecor
detector.reset();
return detectedEncoding;
}