UTF-8 encoding in opencsv

社会主义新天地 提交于 2019-12-13 03:38:37

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/46691554/utf-8-encoding-in-opencsv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!