mdarwin
mdarwin

Reputation: 2394

How do I read tsv files with Jackson csv mapper?

I'm using the Jackson CSV lib to read CSV files.

We've got the streaming example from the documentation :

CsvMapper mapper = new CsvMapper();
mapper.enable(CsvParser.Feature.WRAP_AS_ARRAY);
ObjectReader reader = mapper.readerFor(String[].class);
MappingIterator<String[]> values = reader.readValues("/path/to/file")

This works fine for CSV files. However I can't see how I can configure it to use tab instead of comma as the field delimiter, in order to read TSV files. The only config I can find for a column separator relates to the CsvSchema class, but there is no schema, since we don't know how many columns are in the file.

Upvotes: 1

Views: 4605

Answers (1)

Sachin Gupta
Sachin Gupta

Reputation: 8368

use this:

CsvSchema  schema = mapper.schemaFor(String[].class).withColumnSeparator('\t');
ObjectReader reader = mapper.readerFor(String[].class).with(schema);

Upvotes: 2

Related Questions