Be gentle, This is my first time using Apache Commons CSV 1.7.
I am creating a service to process some CSV inputs, add some additional information from exterior sources, then write out this CSV for ingestion into another system.
I store the information that I have gathered into a list of
HashMap<String, String>
for each row of the final output csv.
The Hashmap contains the <ColumnName, Value for column>
.
I have issues using the CSVPrinter to correctly assign the values of the HashMaps into the rows. I can concatenate the values into a string with commas between the variables; however, this just inserts the whole string into the first column.
I cannot define or hardcode the headers since they are obtained from a config file and may change depending on which project uses the service.
Here is some of my code:
try (BufferedWriter writer = Files.newBufferedWriter(
Paths.get(OUTPUT + "/" + project + "/" + project + ".csv"));)
{
CSVPrinter csvPrinter = new CSVPrinter(writer,
CSVFormat.RFC4180.withFirstRecordAsHeader());
csvPrinter.printRecord(columnList);
for (HashMap<String, String> row : rowCollection)
{
//Need to map __record__ to column -> row.key, value -> row.value for whole map.
csvPrinter.printrecord(__record__);
}
csvPrinter.flush();
}
Thanks for your assistance.