how to know if a file is iso 8859 1 encoded in java

Solutions on MaxInterview for how to know if a file is iso 8859 1 encoded in java by the best coders in the world

showing results for - "how to know if a file is iso 8859 1 encoded in java"
Luca
20 Feb 2018
1FileInputStream fIn  = new FileInputStream(myFile);
2            byte[] buf = new byte[4096];
3            UniversalDetector detector = new UniversalDetector(null);
4            int nread;
5            while ((nread = fIn.read(buf)) > 0 && !detector.isDone()) {
6                detector.handleData(buf, 0, nread);
7            }
8            detector.dataEnd();
9            String encoding = detector.getDetectedCharset();
10            String chartsetName = null;
11            if (encoding.equalsIgnoreCase("WINDOWS-1252")){
12                chartsetName = "ISO-8859-1";
13            }
14            if (encoding.equalsIgnoreCase("UTF-8")){
15                chartsetName = "UTF-8";
16            }
17
18            BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn, chartsetName));