// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class CharSetGuesser {
    public static String guess(String filename) throws IOException {
        int[] freq = new int[256];
        InputStream is = new FileInputStream(filename);
        int c;
        while((c = is.read()) >= 0) {
            freq[c]++;
        }
        is.close();
        if((freq[197] + freq[198] + freq[200] +
            freq[201] + freq[203] + freq[216] +
            freq[229] + freq[230] + freq[232] +
            freq[233] + freq[235] + freq[248]) >
           (freq[133] + freq[134] + freq[136] +
            freq[137] + freq[139] + freq[152] +
            freq[165] + freq[166] + freq[168] +
            freq[169] + freq[171] + freq[184] +
            freq[195])) {
            return "ISO-8859-1";
        } else {
            return "UTF-8";
        }
    }
    public static void main(String[] args) throws Exception {
        System.out.println(guess("C:\\iso-8859-1.txt"));
        System.out.println(guess("C:\\utf-8.txt"));
    }
}
