gpt4 book ai didi

java - 使用不正确的字符集打开 CSV Java 生成的文件

转载 作者:太空宇宙 更新时间:2023-11-04 12:25:31 25 4
gpt4 key购买 nike

下面是用于生成 CSV 文件的 java 代码。尽管在调试此 report.getEncoding() 时获取了“UTF-8”值,但生成的文件使用了错误的字符集。

public void generateFile(ReportBean<T> report, String filename, ReportAggregationPosition reportAggregationPosition) throws IOException {
try (PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(filename), report.getEncoding()))) {
List<T> filteredResult = new ArrayList();
for (T row : report.getResult()) {
if (logRow(row)) {
log.info("Logging row for report={} - {}", filename, getText(row));
}
if (ignoreRow(row)) {
log.info("Ignoring row for report={} - {}", filename, getText(row));
continue;
}
filteredResult.add(row);
}
if (reportAggregationPosition == ReportAggregationPosition.FIRST) {
out.println(filteredResult.size());
}
out.print(getHeader());
if (reportAggregationPosition == ReportAggregationPosition.SECOND) {
out.println(filteredResult.size());
}
for (T row : filteredResult) {
out.print(getText(row));
}
if (reportAggregationPosition == ReportAggregationPosition.LAST) {
out.println(String.format("Total number of rows: %s", filteredResult.size()));
}
}

预期值为:

test-vfde - The hidden edition - Gebühr pro Benutzer Leistungszeit 07/07/16 bis 08/05/16

但显示的值(我无法上传照片,因为我仍然没有积分)如下:

test-vfde - The hidden edition - GebÇŸ’?Ç?¶¬hr pro Benutzer Leistungszeit 07/07/16 bis 08/05/16

字符“ä”或“ü”正确显示。我应该如何处理这个问题?

最佳答案

尝试添加以下行

.....
try (PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(filename), report.getEncoding()))) {
out.write(0xef);
out.write(0xbb);
out.write(0xbf);
List<T> filteredResult = new ArrayList();
.....

描述可以找到here

关于java - 使用不正确的字符集打开 CSV Java 生成的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38438996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com