简体   繁体   中英

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? Eg 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!

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM