Super CSV is a free library for reading and writing
CSV files in Java. We need to have SuperCSV-1.52.jar, spiffy-0.05.jar or
later versions in classpath.
The following example shows reading a CSV as list of maps using Super CSV.
The CSV header name is used as key and column data as value.
//Read CSV Header
List<String> header = new ArrayList<String>(csvReader.read());
List<String> rowAsTokens;
//Read the CSV as List of Maps where each Map represents row data
List<Map<String, String>> rows = new ArrayList<Map<String, String>>();
Map<String, String> row = null;
while ((rowAsTokens = csvReader.read()) != null) {
//Create Map for each row in CSV
row = new HashMap<String, String>();
for (int i = 0 ; i < header.size() ; i ++) {
row.put(header.get(i), rowAsTokens.get(i));
}
//add Row map to list of rows
rows.add(row);
}
//Iterate for (Map<String, String> rowMap : rows) {
System.out.println(rowMap);
}