Problem while reading french word from text file in java / android

I am trying to read the contents of a file in French (character by character) and check the ascii value to perform some operation. Everything works fine, with the English alphabet, but for a character like éééé, I am facing some problem.

For example, if the contents of my file are français, I get output as franãais. Here, I am attaching my code, please take a look and advise me to fix this problem.

File file = new File("C:\text.txt");

fis = new BufferedInputStream(new FileInputStream(file));

char current;
char org;
while (fis.available() > 0) {
    current = (char) fis.read(); // to read character
                                    // from file
    int ascii = (int) current; // to get ascii for the
                                // character
    org = (char) (ascii); // to get the actual
                                // character

    if (ascii == 10) {          
        resultString = resultString.append(",'"
                    + strCompCode + "'");
        dbhelpher.addDataRecord(resultString.toString());

        resultString.setLength(0);
    } else if (ascii != 13) { // other than the ascii
                                // 13, the character are
                                // appended with string
                                // builder
        resultString.append(org);
    }
}
fis.close();

Here I need to read the French char, as it is in the text file. Your advice will be greatly appreciated. Thanks in advance.

+4
source share
1

InputStreamReader UTF8:

InputStreamReader reader = new InputStreamReader(fis, "UTF8");

IO Apache Commons. , for:

List<String> lines = IOUtils.readLines(fis, "UTF8");

for (String line: lines) {
  dbhelper.addDataRecord(line + ",'" + strCompCode + "'"); 
}

build.gradle :

dependencies {
  ...
  compile 'commons-io:commons-io:2.4'
  ...
}
+4

Source: https://habr.com/ru/post/1609281/


All Articles