title: Java : How to determine the correct charset encoding of a stream
body: <p>With reference to the following thread:
  <a href="https://stackoverflow.com/questions/498636/java-app-unable-to-read-iso-8859-1-encoded-file-correctly">Java App : Unable to read iso-8859-1 encoded file correctly</a></p>
  
  <p>What is the best way to programatically determine the correct charset encoding of an inputstream/file ?</p>
  
  <p>I have tried using the following:</p>
  
  <pre><code>File in =  new File(args[0]);
  InputStreamReader r = new InputStreamReader(new FileInputStream(in));
  System.out.println(r.getEncoding());
  </code></pre>
  
  <p>But on a file which I know to be encoded with ISO8859_1 the above code yields ASCII, which is not correct, and does not allow me to correctly render the content of the file back to the console.</p>