Alagammal P
Alagammal P

Reputation: 909

UTF-8 encoding in opencsv

I am working with opencsv(2.3), i create the header of the csv from the properties file which contains special chracters.

I encode using UTF-8 and set the property as a header to csv.

But I still see CSV file created doesnt reflect the encoding.

(I used the same approach to create PDF using jasperreports, there i could see the columns with special characters are encoded well and shown properly)

Java version : 7

properties file content: (é is getting stored as é and ü is stored as ü)

lan.response=Nombre de réponses

lan.exef=Exécution

lan.exeg=Ausführung

Approach using csv:

/* properties file*/
 final File  propertiesFile = new File(System.getProperty("user.home"), "tmp/lan.properties");
 final FileInputStream fis= new FileInputStream(propertiesFile);

 final InputStreamReader inputStreamReader = new InputStreamReader(fis, "UTF-8");

 final Properties properties= new Properties();
 properties.load(inputStreamReader);

 properties.list(System.out);

 /*CSV Header*/
 StringBuilder header = new StringBuilder();
 header.append(properties.get(lan.response)).append(",");
 header.append(properties.get(lan.exef)).append(",");
 header.append(properties.get(lan.execg));

 String[] colHeader = header.split(",");

  FileOutputStream fos =  new FileOutputStream(fileName);
  Writer fw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
  CSVWriter csvWriter = new CSVWriter(fw, ";");

  // add header
  csvWriter.writeNext(colHeader);
  // add data 
  String[] col= new String[3];

   for(Customer c : customerList) {
         col[0] = c.getCustomerName();
         col[1] = c.getCustomerId();
         col[2] = c.getCustomerBirthDate();
         csvWriter.writeNext(col);
   }

   csvWriter.close();

Can anyone please help me as I am able to see the special characters in other file formats(Ex: PDF) not in CSV?

Upvotes: 0

Views: 19001

Answers (1)

Scott Conway
Scott Conway

Reputation: 983

When running your application start it with the -Dfile.encoding=UTF-8. You can see some options in this stackoverflow question. My concern here is that you are forcing your input and output to be UTF-8 but the String and StringBuilders you are creating in between are using the system default encoding, which in your case seems to be ISO-8859-1. I am wondering if switching from UTF-8 to ISO-8859-1 back to UTF-8 is the problem.

Upvotes: 2

Related Questions