public String detectFileCharset(File file) throws FileNotFoundException, IOException { detector.Init( new nsICharsetDetectionObserver() { @Override public void Notify(String arg0) { found = true; charset = arg0; } }); byte[] buffer = new byte[2048]; int length; BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file)); boolean done = false; boolean isAscii = true; while ((length = stream.read(buffer, 0, buffer.length)) != -1) { if (isAscii) { isAscii = detector.isAscii(buffer, length); } if (!isAscii && !done) { done = detector.DoIt(buffer, length, false); } } detector.DataEnd(); stream.close(); if (isAscii) { charset = "us-ascii"; found = true; } if (!found) { return null; } return charset; }