Search code examples
javacharacter-encodingfileutilsapache-commons-io

ö character encoding issue in CSV file creation


I am trying to create a CSV file from my Java code.

    File file = File.createTempFile("DummyReport", ".csv");
    SomeListofObjects items = getSomeList();
    FileUtils.write(file, "ID;CREATION;" + System.lineSeparator());
    FileUtils.writeLines(file, activities.getItems(), true);        
    return file;

I am facing some issue with special chars.

When I debug the code, I found that I have a character as "ö". But in the csv file generated, it is coming weirdly "ö".

Can we set this in FileUtile or File? Can some one help me to solve this?


Solution

  • First check if you are using a text viewer that displays your output correctly. If not, the problem might be your system encoding.

    FileUtils.write(file, string) uses the default system encoding, which in your system seems to be 8bit. The "ö" character however is encoded as two bytes, resulting in "ö.".

    Use FileUtils.write(File file, CharSequence data, String encoding) instead, with an appropriate encoding:

    • ISO 8859-1 (8bit standard, Latin-1)
    • CP1252 (8bit proprietary, Windows default, extends Latin 1)
    • MacRoman (8bit proprietary, Apple default)
    • UTF-8 (16bit standard, Linux default)
    • Latin-15 (not always supported)

    My suggestion is to use FileUtils.write(file, string, "UTF-8").