0

My Use case is to edit a CSVRecord. I am reading a csv file and need to edit only one column in record on conditional basis. If condition satisfies then need to edit and print to another file else need to print as it is. Any help in how to achieve this. Thanks in Advance.

Reader reader = Files.newBufferedReader(Paths.get(inputFilePath),  StandardCharsets.UTF_8);
            CSVFormat format =  CSVFormat.EXCEL.withFirstRecordAsHeader()
                    .withIgnoreHeaderCase().withDelimiter(SEPERATOR).withIgnoreSurroundingSpaces()
                    .withRecordSeparator(NEW_LINE_SEPARATOR)
                    .withTrim();
            CSVParser csvParser = new CSVParser(reader,format);

Something like below

for (CSVRecord csvRecord : csvParser) { 

if (someCondition) {
replace "Status" in csvRecord from Not OK to OK
then csvPrinter.printRecord(csvRecord); 
  } else {
  csvPrinter.printRecord(csvRecord);
} }
JensG
  • 13,148
  • 4
  • 45
  • 55
SRana
  • 125
  • 1
  • 12
  • Possible duplicate of [what is the best way to edit csv file](https://stackoverflow.com/questions/3110786/what-is-the-best-way-to-edit-csv-file) – Akiner Alkan Jan 08 '19 at 08:41
  • I am looking for way to edit direct CSVRecord and using commons csv – SRana Jan 08 '19 at 10:01
  • commons-csv is not built for this use-case, the CSVRecord object is read-only, only CSVParser can construct such an object, so without putting a derived class into the org.apache.commons.csv namespace you cannot do this cleanly. – centic Jan 11 '19 at 15:02

0 Answers0