Below is the java code used to generate the CSV file. Although while debugging this report.getEncoding()
acquires the value of "UTF-8", the file is generated with an incorrect charset.
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()));
}
}
The expected value is:
test-vfde - The hidden edition - Gebühr pro Benutzer Leistungszeit 07/07/16 bis 08/05/16
But the value that is shown (I couldn't upload a photo as I still don't have credits) is the following one:
test-vfde - The hidden edition - GebÇŸ’?Ç?¶¬hr pro Benutzer Leistungszeit 07/07/16 bis 08/05/16
Characters as "ä" or "ü" are shown correctly. How should I approach this?