• Post Reply Bookmark Topic Watch Topic
  • New Topic

Updation of any Text File  RSS feed

 
Rajesh Kumar Swain
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to update any text file by simple replacing one line with another new line. Currently I am using MappedByteBuffer to do the task.

If the replaced line and replacing line are of same length then it is working fine.

But if the length is different then It is disturbing the next line.Which I do not want.

So can anyone suggest some thing so that I can do the above task without affecting any line(previos or next).

Thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're pretty much out of luck. You'll have to read the file and write a new version. Here's a neat technique from the old VM/CMS operating system:

That way you always have some good version of the file on disk in case something goes wrong in mid stream.

Ok, there are tricks you can do with RandomAccessFile but it gets nasty. If you have to make a line longer, read the bytes after the line and rewrite them a few bytes further down the file, making a hole for the new stuff. If the tail end of the file is too big to read into one buffer, you may have to start at the end and move one buffer at a time over a few bytes. Making a line shorter and closing the hole is much the same but I'm not sure how to truncate the file at the end. All in all, the code is pretty complex to write (or read), and it doesn't have the backup if anything goes wrong.
 
Rajesh Kumar Swain
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,

Thanks a lot for your help.

I have tried in the way you suggested . Now It's working fine.


Regards
Rajesh
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!