1 year ago
#338907
gstackoverflow
How to read CSV without header and unknown amount of columns?
I have a class which parses CSV file. This CSV file doesn't have header. Also it mus contain only 1 column.
I want to add validation for columns count.
My current code is:
CsvSchema csvSchema = CsvSchema.builder().addColumn("MY_COLUMN").build();
try (MappingIterator<Map<String, String>> iter = csvMapper.readerFor(Map.class)
.with(schema)
.readValues(new InputStreamReader(inputStream))) {
....
while (iter.hasNextValue()) {
Map<String, String> map = iter.next();
...
}
}
At this case map contains only 1 key/pair so it sees only first column. If there are 2, 3 or more columns - all of them are ignored. So this code doesn't allow me to understand amount of columns.
I tried to use another schema:
CsvSchema csvSchema = CsvSchema.emptySchema()
.withUseHeader(false)
.withComments()
.withColumnSeparator(columnSeparator);
But it lead to the parsing error:
Cannot deserialize value of type `java.util.LinkedHashMap<java.lang.Object,java.lang.Object>` from Array value (token `JsonToken.START_ARRAY`)
at [Source: (InputStreamReader); line: UNKNOWN, column: UNKNOWN]
Could you please give me a hint how to achieve my aim ?
java
csv
jackson
fasterxml
0 Answers
Your Answer