• Post Reply Bookmark Topic Watch Topic
  • New Topic

modifying file at run time...  RSS feed

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am facing one problem. Please guide me.

Suppose I have a file, the contents of file: Hello how are you

Now if I want to replace 'how' with 'who' (Hello who are you), no shift is required in this case.

Now replacing 'how' with 'a' (Hello a are you), new word is smaller than old one, so left shift is required.

Same when new word is longer than old word, right shift is required for some character for example: 'how' -> 'people'

Hello how are you
Hello people are you

I can't use any temporary file, no buffer.

I hope my problem is clear. Please help me in this, what should I use for this and how?

Thanks a lot.
[ September 28, 2005: Message edited by: rathi ji ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The restriction of no temp file or buffer sounds like a puzzle or a school assignment.

Imagine you have a row of four pegs on the wall with coats hanging on the first three and the last one is empty. How would you insert a new coat between the 1st and 2nd? Probably shift the 3rd one over one spot, then the 2nd one over one spot.

Look at RandomAccessFile and see if you can find a way to shift a block of bytes to the right. There are no empty spots to the right in a file ... what can you do to add more pegs on the wall?

This is an approach I'd use only on a puzzle or assignment with the constraints you gave. In the wild I'd use a temp file.
 
Norm Radder
Rancher
Posts: 1734
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't imagine how you could do it without a buffer. You have to read enough of each section of the file into memory to see if it contains a string to be removed/replaced. What is read is in a buffer.

What do you mean by a buffer?

A way to look at the problem is to imagine the file as a string and you are looking at it thru a small window that only shows a few characters at at time. As you move the view to new characters (say from left to right) you write the characters that pass out of view to the file. Initially you don't need to write until you make changes to the data. When you see something in the window that needd to be changed, change it before it passes out of sight. Once you change bytes you'll need to start writing. There would be an optimization if the length of the new data was the same as the old - you could stop writing as soon as the changes were written.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I can't buffer the whole content of file, though I can buffer it partially...

Thanks.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!