1

I need to write in a specific cell in a .csv file with org.apache.commons.csv library whitout iterate and copy all lines.

I tried this:

Reader reader = Files.newBufferedReader(Paths.get(Properties.dataFilePath));
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT);
List<CSVRecord> records = csvParser.getRecords();
records.get(rowNumber)[columnIndex] = value;

However it is not allowed.

Edit 1

My IDE mentioned : The type of the expression must be an array type but it resolved to CSVRecord

Royce
  • 1,557
  • 5
  • 19
  • 44

1 Answers1

1

have you tried it with value = records.get(rowNumber).get(columnIndex);?

Have a look here, it may help you: https://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVRecord.html

Andree Kröger
  • 84
  • 1
  • 1
  • 5
  • Yes but I'm not able to set something with `get` method. – Royce Oct 30 '19 at 11:17
  • 1
    Sorry, I didn't understand your first try not correctly. – Andree Kröger Oct 31 '19 at 08:02
  • 2
    You can't override the value in a CVSRecord, it's only for reading. If you want to change content of your CSV-File, you have to read with CSVRecoed the content of your file, modify it and then you have to write it with CSVPrinter to your new file. Read: https://commons.apache.org/proper/commons-csv/user-guide.html#Example:_Parsing_an_Excel_CSV_File Print: https://commons.apache.org/proper/commons-csv/user-guide.html#Printing_with_headers – Andree Kröger Oct 31 '19 at 08:21