csvspecial-characterssuperscriptsupercsvunicode-escapes

Superscript characters are resulting in to junk characters in generated CSV file when using ICsvListWriter


I am trying to write superscript characters in .csv file. I am using method write(List<?> columns)of org.supercsv.io.ICsvListWriter. In generated .csv file the superscript character is coming along with junk character before it.

List columns = new ArrayList();
String myString = "abcd1";
columns.add(myString.replaceAll("1", "¹"));
csvWriter.write(columns);

In the generated .csv file it is coming as

abcd¹

I also tried with unicode but it is not helping.

columns.add(myString.replaceAll("1", "\u00B9"));

Any suggestion here please?


Solution

  • Found a solution for this problem. Correction was needed in creating ICsvListWriter object. Previously I was having this code where 'response' is HttpServletResponse.

    CsvPreference preference = new CsvPreference.Builder(CsvPreference.STANDARD_PREFERENCE).useEncoder(new DefaultCsvEncoder()).build();
    ICsvListWriter csvWriter = new CsvListWriter(response.getWriter(), preference);
    

    This code is enhanced to this:

            ServletOutputStream output = response.getOutputStream();
            output.write(new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF });
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, "UTF-8"));
            CsvPreference preference = new CsvPreference.Builder(CsvPreference.STANDARD_PREFERENCE).useEncoder(new DefaultCsvEncoder()).build();
            ICsvListWriter csvWriter = new CsvListWriter(writer, preference);
    

    This fixed the issue and all of the superscript characters are now coming properly in generated CSV file without any junk characters. No mater whether I use actual superscript characters or their Unicode, this fix works.