Example #1
0
 public static Charset guessEncoding(File f, int bufferLength, Charset defaultCharset)
     throws FileNotFoundException, IOException {
   FileInputStream fis = new FileInputStream(f);
   byte[] buffer = new byte[bufferLength];
   fis.read(buffer);
   fis.close();
   CharsetToolkit toolkit = new CharsetToolkit(buffer);
   toolkit.setDefaultCharset(defaultCharset);
   return toolkit.guessEncoding();
 }
Example #2
0
  public static void main(String[] args) throws FileNotFoundException, IOException {
    //		File file = new File("utf-8.txt");
    File file = new File("windows-1252.txt");

    Charset guessedCharset = CharsetToolkit.guessEncoding(file, 4096);
    System.err.println("Charset found: " + guessedCharset.displayName());

    FileInputStream fis = new FileInputStream(file);
    InputStreamReader isr = new InputStreamReader(fis, guessedCharset);
    BufferedReader br = new BufferedReader(isr);

    String line;
    while ((line = br.readLine()) != null) {
      System.out.println(line);
    }
  }