I have a very simple tsv file, with entries as so:
614 2006-07-13 15:30:05 2009-11-20 23:56:21 510 350 3265 10 34
1038 2006-07-15 16:12:15 2009-11-16 05:12:11 304 443 4405 7 156
1437 2006-07-16 12:29:24 2009-11-16 16:25:12 45 73 725 6 37
2615 2006-07-19 23:23:55 2009-11-27 18:34:36 211 230 211 7 0
3148 2006-07-26 14:17:22 2009-11-20 17:35:18 7346 7244 11438 8 97
5593 2006-09-08 10:58:49 2009-11-24 06:08:27 898 1024 2897 8 56
It does not have headers, and I'm getting it from another source, so I have no control over how it's written. I want to read in the first column, do something with it, and ignore the rest.
My code is:
List<Long> userIds = new ArrayList<Long>();
ICsvMapReader mapReader = null;
try {
mapReader = new CsvMapReader(new FileReader(inFile), CsvPreference.TAB_PREFERENCE);
// only map the first column - setting header elements to null means those columns are ignored
final String[] header = new String[] { "userid", null, null, null, null, null, null };
final CellProcessor[] processors = new CellProcessor[] {null,
null,
null,
null,
null,
null,
null };
Map<String, Object> userMap;
while( (userMap = mapReader.read(header, processors)) != null ) {
Long userId = Long.parseLong(userMap.get("userid").toString());
userIds.add(userId);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
IOUtils.closeQuietly(mapReader);
}
I get no exceptions, but the mapReader.read() line always returns null. I tried using new ParseLong() in place of null in the first position of the processor, and that had no effect. I feel like I'm missing something really basic.
Your code works fine for me, although I had to add an extra null
element to the header and processors as there's actually 8 columns in your data. Otherwise Super CSV throws an exception:
org.supercsv.exception.SuperCsvException: The number of columns
to be processed (8) must match the number of CellProcessors (7):
check that the number of CellProcessors you have defined matches
the expected number of columns being read/written
context={lineNo=1, rowNo=1, columnNo=1, rowSource=
[614, 2006-07-13 15:30:05, 2009-11-20 23:56:21, 510, 350, 3265, 10, 34]}
I'd check that you're reading in the correct file - it sounds like you're reading in an empty file...