Search code examples
javaandroidcsvfile-handlingopencsv

OpenCSV reads strange text out of file


I am using AndroidStudio and my applications has to read in a CSV file which looks like this:

"Anmeldung";"1576017126809898";"1547126680978123";"";"";"Frau"
"Anmeldung";"1547126680911112";"1547126680978123";"";"";"Frau"

But as you can see in the following picture, OpenCSV reads some strange character and in my List there are senseless Strings which are not in the file it read

strange data

This is how I read the Data out of my file:

        try {
        FileReader filereader = new FileReader(filePath);
        CSVParser parser = new CSVParserBuilder().withSeparator(';').build();

        CSVReader csvReader = new CSVReaderBuilder(filereader)
                .withSkipLines(1)
                .withCSVParser(parser)
                .build();
        List<String[]> allData = csvReader.readAll();
        MainActivity.setAllData(allData);

    }
    catch (Exception e) {
        e.printStackTrace();
    }

Thank you


Solution

  • I figured it out. It might sound strange but I took the file and replaced all ; with ; I think the data I got were exportet with an UTF-16 Encoding or from an linux device.

    tl;dr The File had the wrong encoding. And the way I opened and viewed it was correct