Skip to content

Instantly share code, notes, and snippets.

@cowtowncoder
Created December 9, 2011 22:48
Show Gist options
  • Save cowtowncoder/1453666 to your computer and use it in GitHub Desktop.
Save cowtowncoder/1453666 to your computer and use it in GitHub Desktop.
Reading CSV data as Maps
private int readAll(File inputFile) throws IOException
{
int count = 0;
CsvMapper mapper = new CsvMapper();
// need a Schema just to force use of header line (to get column names)
CsvSchema schema = CsvSchema.builder()
.setUseHeader(true)
.build();
// read CSV as sequence of Objects, binding as regular Map (not POJO)
MappingIterator<Map<?,?>> it = mapper.reader(Map.class)
.withSchema(schema).readValues(inputFile);
while (it.hasNext()) {
Map<?,?> row = it.nextValue();
// and maybe do something with it...
++count;
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment