3

I want to parse the following CSV File:

02.01.15,Januar,GEZ,Telekommunikation,"€ 211,44",01_jährlich,Ist
02.01.15,Januar,BGV: Haftpfl. + Unfall,Versicherungen,"€ 171,95",01_jährlich,Ist

I use the following code for parsing:

  CSVReader csvReader = new CSVReader(new FileReader(file));
            String [] nextLine;
            while ((nextLine = csvReader.readNext()) != null) {
                for (String text : nextLine) {
                    System.out.print(text + " ");
                }
                System.out.println();
            }

The output shows a vertically centered dot at the beginning of the first line. What is the problem?

Cellist
  • 89
  • 2
  • 11

2 Answers2

1

This is caused by a UTF-8 signature at the beginning of the file (looking at the data, your file is probably encoded in UTF-8).

https://en.wikipedia.org/wiki/Byte_order_mark

The bytes 0xEF, 0xBB, 0xBF ( in ASCII), convert to a centered dot in UTF-8.

You might have to trim that away (or check if you can provide a parameter to CSVReader or FileReader to skip those bytes). See here for some more information.

Danny_ds
  • 11,201
  • 1
  • 24
  • 46
0

Because when you check the condition in the while loop you call the readNext() method. This consumes the first row without using it. If you tell me what library you are using I can tell you what else you could do. Usually in Java there's a hasNext() method for this purpose.

I suppose you're using OpenCSV library, in this case you could do this way

First import the Arraylist library import java.util.ArrayList;

CSVReader csvReader = new CSVReader(new FileReader(file));
ArrayList<String> lines = new ArrayList<String>();
lines = csvReader.readAll();
for(String line : lines) {
    System.out.println(line + " ");
}

PS: if you want to print the lines in the same row you should use System.out.print(line + " "); because it doesn't make any sense adding the space after the line and then going to a new line (println())

Luca Mozzo
  • 892
  • 2
  • 8
  • 20