opencsv中的java UTF8编码
我正在使用opencsv(2.3),我从包含特殊字符的属性文件创建csv的标题
我使用UTF-8编码,并将属性作为头设置为csv
但我仍然看到创建的CSV文件没有反映编码
(我使用相同的方法使用jasperreports创建PDF,在那里我可以看到带有特殊字符的列被很好地编码并正确显示)
Java版本:7
属性文件内容:(é存储为 é和ü存储为ü)
兰。响应=响应名称
兰。exef=执行
兰。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中的特殊字符
# 1 楼答案
运行应用程序时,请使用-Dfile启动它。编码=UTF-8。您可以在this stackoverflow question中看到一些选项。我在这里担心的是,您正在强制输入和输出为UTF-8,但是您正在创建的字符串和StringBuilder使用的是系统默认编码,在您的情况下,它似乎是ISO-8859-1。我想知道从UTF-8切换到ISO-8859-1再切换到UTF-8是否是问题所在