1

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?

Sentry
  • 4,102
  • 2
  • 30
  • 38

1 Answers1

0

Try to add following lines

.....
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();
    .....

Description can be found here

Community
  • 1
  • 1
Eritrean
  • 15,851
  • 3
  • 22
  • 28
  • Uzochi. Thankx for the answer. I realized that in many parts of the application the same code is applied before the content is written... The thing here is that I can't reproduce it so I'll have to think in a way of testing this but not locally... I'll keep you update my friend... cheers – Agustn Ernesto Cardeilhac Bans Jul 18 '16 at 14:58