Tuesday, August 4, 2009

Java FileWriter UTF-8 encoding

Not possible to specify encoding for a FileWriter object.
The documentation states that FileWriter is a convenient class depending on the underlying platform.
The easiest solution is to use an OutputStreamWriter. Something as follows, in my case I am exporting to CSVWriter:

FileOutputStream fos = new FileOutputStream(new File(fileName));
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
CSVWriter writer = new CSVWriter(osw, CSV_DELIMITER);

2 comments:

  1. Thanks...Something I was looking for!

    ReplyDelete
  2. everything fine but it not working

    ReplyDelete