Using the replace method with CsvWriter with Java

575 views
Skip to first unread message

Eric

unread,
Feb 26, 2015, 10:54:09 PM2/26/15
to csv...@googlegroups.com
Im using the csv reader/writer to write some simple records which contain some information including balances. I want to update the balance for some clients without writing new lines to the file (i only want 1 line for each unique client ID).

I didnt find any other methods to do this other than using replace to replace a string in the file.

How does this method work,

public static String replace(String original, String pattern, String replace)

shriop

unread,
Feb 26, 2015, 11:22:59 PM2/26/15
to csv...@googlegroups.com
Just FYI, posts are moderated in this forum, which is why your post was delayed until I approved it to post.

Interestingly, I was not aware that the replace method was public. It should have been private. I do not think it will be helpful for what you're trying to accomplish. It's really just a helper method for a shortcut to replace all occurrences of a string within another string with the replacement string.

If I understand what you're asking for, I would suggest instantiating a CsvReader object pointing at the original file, and a CsvWriter object pointing at a new file. Read each record and then write each record back out, changing the balance that you're writing out if it matches your client id you're looking for. When you're done writing out the new file, swap the original for the new, either saving the original or just deleting it.

Bruce Dunwiddie
Reply all
Reply to author
Forward
0 new messages