How do I read tsv files with Jackson csv mapper?

為{幸葍}努か 提交于 2020-05-23 11:51:10

问题


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.


回答1:


use this:

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


来源:https://stackoverflow.com/questions/46919012/how-do-i-read-tsv-files-with-jackson-csv-mapper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!