• Post Reply Bookmark Topic Watch Topic
  • New Topic

Delete a line in text file

 
Praveen Seluka
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I want to delete some line in the middle of a text file.
Except writing it to a temporary file I dint get any efficient way.

Got some workarounds but performance is the issue here
What if I have million lines,I cant move everything.(inefficient)
I have a solution but at some design cost.
Any way for this?


Thanks
Praveen SP
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To remove data in the middle of a file, requires rewriting the file, at least all of the data that follows the deleted section up over where the deleted data was. For example given a String: ABC, to delete the B, copy the C forward over the B and truncate(RandomAccessFile.setLength) the file following the new location of C.
 
Praveen Seluka
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya what if my file has millions of records and if the line to be deleted is 10th line.The move operation is very costly here.Any work around?
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Praveen Seluka:
Ya what if my file has millions of records and if the line to be deleted is 10th line.The move operation is very costly here.Any work around?


Is it fine to replace the deleted line with a blank line? i.e. just to erase the characters from the line?
If you are fine with it, then, use a RandomAccessFile in a read+write mode.
You can read one line at a time and when you know that you have to delete a line, use seek to go back to the start of the line. Write spaces equal to the number of characters in the line.
This will not be very easy and straight forward but i am sure it is possible.
Let know what you did.

On the other hand, you can use FileChannels' transferTo and transferFrom methods to effeciently move data from one file to another.
In my opinion, using a temporary file is the cleanest way and unless absolutely necessary you must not look for workarounds.

Remember: "Premature optimization is the root of all evils"
 
Praveen Seluka
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

Thanks for your reply.The main reason I want to delete the line in the middle is to save space.As my file is big,I will be deleting a lot of unwanted lines.Now over-writing that with the space is possible,But it doesnt help me as my main motive to delete a line is to reduce the file size.

Still any work arounds?



Thanks
Praveen SP
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't have your cake and eat it too.
To change the size of a file by removing data in the middle of if requires writing!!! Period, full stop.
A way to keep from rewriting too ofter would be to batch the updates and only do them periodically. Use RandomAccessFile to overwrite the deleted data on demand, but not rewrite the whole file.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!