I have a csv file like the below one and need to parse the file using Apache commons CSV Parser , not record by record but column by column
Column A Column B Cell 1 Cell 2 Cell 3 Cell 4
The desired result I want to read like is
Column A , Cell 1 ,Cell 3
Column B , Cell 2, Cell 4
Is there anyway that CSV Parser allows to read like this . I foreseen in the documentation that it only reads record by record.
That’s right – it only reads records by record.
So how to get what you want? Many ways to do this, e.g.
- if you are confident the data will easily fit in memory, and if the data is “square” then you could create 2 dimension array structure like this:
String[][] data = new String[colCount][rowCount];
, transcribe the data into the array in one “direction” and read it out in the other - if your data is very large, consider using an external storage option, like a database or MapDB