问题
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