2

I am using apache commons CSV parser to convert the CSV to a map. In the map I couldnt able to read some values through intellij debuger. if I manually type map.get("key") the value is null. However, if I copy paste the key from the map, I am getting data. Couldnt understand what is going wrong. Any pointers would help. Thanks

Here is my CSV parser code:

 private CSVParser parseCSV(InputStream inputStream) {
        System.out.println("What is the encoding "+ new InputStreamReader(inputStream).getEncoding());
        try {
            return new CSVParser(new InputStreamReader(inputStream), CSVFormat.DEFAULT
                    .withFirstRecordAsHeader()
                    .withIgnoreHeaderCase()
                    .withSkipHeaderRecord()
                    .withTrim());
        } catch (IOException e) {
            throw new IPRSException(e);
        }
    }
Minisha
  • 2,117
  • 2
  • 25
  • 56

2 Answers2

5

There was a weird character in the strings (Reference: Reading UTF-8 - BOM marker). The below syntax help to resolve the issue

header = header("\uFEFF", "");
Minisha
  • 2,117
  • 2
  • 25
  • 56
2

in java use UnicodeReader:

String path = "demo.csv";
CSVFormat.Builder builder = CSVFormat.RFC4180.builder();
CSVFormat format = builder.setQuote(null).setHeader().build();

InputStream in = new FileInputStream(new File(path));
CSVParser parser = new CSVParser(new BufferedReader(new UnicodeReader(in)), format);
luke
  • 21
  • 1