opencsv中的UTF-8编码 [英] UTF-8 encoding in opencsv

查看:629
本文介绍了opencsv中的UTF-8编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用opencsv(2.3),我从包含特殊特征的属性文件中创建了csv的标头.

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

我使用UTF-8进行编码,并将该属性设置为csv的标头.

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

但是我仍然看到创建的CSV文件不能反映编码.

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

(我使用相同的方法使用jasperreports创建PDF,在那里我可以看到带有特殊字符的列已正确编码并可以正确显示)

(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版本:7

属性文件内容:(é正在存储为 é和ü存储为¼)

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

lan.response =反应无果

lan.response=Nombre de réponses

lan.exef =执行

lan.exef=Exécution

lan.exeg =Ausführung

lan.exeg=Ausführung

使用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();

任何人都可以帮助我,因为我可以看到其他文件格式(例如PDF)中非CSV格式的特殊字符吗?

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

推荐答案

运行应用程序时,请使用-Dfile.encoding = UTF-8启动它.您可以在此stackoverflow问题中看到一些选项.我在这里担心的是,您将输入和输出强制为UTF-8,但是在两者之间创建的String和StringBuilders使用的是系统默认编码,在您的情况下,该编码似乎是ISO-8859-1.我想知道是否是从UTF-8到ISO-8859-1切换回UTF-8的问题.

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.

这篇关于opencsv中的UTF-8编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆