write UTF-8 BOM with supercsv

allaf picture allaf · Aug 18, 2015 · Viewed 9.6k times · Source

I am using supercscv to write an utf-8 encoded csv. It produces a normal file but excel doesn't recognize it as utf-8 cause it's dumb, excel lost without the bom marker so any special characters are corrupted when opened with excel.

Is there a way to write a file as UTF-8 with BOM with supercsv ? I can't find it.

Thanks

Answer

Joop Eggen picture Joop Eggen · Aug 18, 2015

As supercsv probably wraps a Writer:

Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
writer.write('\uFEFF'); // BOM for UTF-*
... new BeanWriter(writer, CsvPreference.STANDARD_PREFERENCE);