* @throws IOException
*/
public static String getFileCharset(File file) throws IOException {
byte[] buf = new byte[4096];
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
final UniversalDetector universalDetector = new UniversalDetector(null);
int numberOfBytesRead;
while ((numberOfBytesRead = bufferedInputStream.read(buf)) > 0 && !universalDetector.isDone()) {
universalDetector.handleData(buf, 0, numberOfBytesRead);
}
universalDetector.dataEnd();
bufferedInputStream.close();
String encoding = universalDetector.getDetectedCharset();
if (encoding != null) {
logger.debug("Detected encoding for {} is {}.", file.getAbsolutePath(), encoding);
} else {
logger.debug("No encoding detected for {}.", file.getAbsolutePath());
}
universalDetector.reset();
return encoding;
}