final UniversalDetector universalDetector;
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file))) {
universalDetector = new UniversalDetector(null);
int numberOfBytesRead;
while ((numberOfBytesRead = bufferedInputStream.read(buf)) > 0 && !universalDetector.isDone()) {
universalDetector.handleData(buf, 0, numberOfBytesRead);
}
}
universalDetector.dataEnd();
String encoding = universalDetector.getDetectedCharset();