How do I count the number of columns in a CSV file using Apache Commons CSV? My goal is to generate headers for the CSVParser, so long as I can determine the number of columns.
Example CSV file:
0,1,0
1,2,3
2,3,4
...
In this case the number of columns would be three. So what I'm looking for is something like:
CSVParser parser = format.parse(file);
CSVRecord firstRecord = parser.iterator().next();
int numColumns = firstRecord.size();
Note: I seem to be able to parse files just fine as long as they have an extra line at the beginning and I create a format, as such:
file:
asdf
0,1,0
1,2,3
2,3,4
...
format:
CSVFormat = format.withFirstRecordAsHeader().withSkipHeaderRecord()