• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

modifying file at run time...

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I can't buffer the whole content of file, though I can buffer it partially...

Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic