• Post Reply Bookmark Topic Watch Topic
  • New Topic

Removing a line of text from a csv file  RSS feed

 
david allen
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am working on a csv file. Each record in the file has an ID number. I need to be able to delete a record based on this number. Is there anyway that one can delete a record from a csv file in java.
I know some might say use a database but I cant.
Any ideas???
Thank you
david
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no way to delete a line of text from any file except to copy the whole file, omitting the line in question.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no way to delete a line of text from any file except to copy the whole file, omitting the line in question.
Well I agree this is usually the preferred way. However there are a couple other options I can think of. If you're reasonably close to the end of the file, you can use a RandomAccessFile or FileChannel to rewrite the latter part of the file only, leaving the first part untouched. Or you could use RAF or FileChannel to simply replace the old line with spaces, or newlines, or some other symbol which indicates "deleted" (assuming that your CSV parsing can deal with a line of spaces or whatever). This could be useful if the CSV file is really big and performance is a problem. You could periodically clean up the file by rewriting the whole thing, omitting any deleted records.
 
Richard Rodger
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to have a look at http://www.csv-jdbc.com
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!