ss1
ss1

Reputation: 1191

How to replace a specific row from a CSV file?

Is it possible to access a specific row in a CSV file and replace a specific field in it? E.g. like in this pseudo code:

Row row = csvFile.getRow(123);
row.getField(2).set("someString");

Tried Apache Commons CSV and OpenCSV, but can't figure out how to do it with them. Only way I can think of is to iterate through a file, store everything in a new object and create a file, but that doesn't seem like a good way.

Thanks for any suggestion!

Upvotes: 0

Views: 2733

Answers (1)

Scott Conway
Scott Conway

Reputation: 983

Sounds like a duplicate of this one to me.

I am not sure about how to do it with apache but I am sure they have the same mechanisms. In openCSV you would create an CSVReader to your source file and a CSVWriter to your destination file. Then read a record at a time, make any desired changes and write it out.

Or, if the file is small, you can do a readAll and writeAll.

But if you want to do it all in the same file read my comments in the duplicate.

Upvotes: 1

Related Questions