Created
December 9, 2011 22:48
-
-
Save cowtowncoder/1453666 to your computer and use it in GitHub Desktop.
Reading CSV data as Maps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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