2

In a Random Access File, at a particular byte position, the new text to be inserted is overwritten. How do I modify the write method so that the new text is inserted between the existing values without overwriting?

I know it is possible to shift but I 'know how the logic. Please help me with the logic

Java Enthusiast
  • 1,161
  • 2
  • 15
  • 30

2 Answers2

2

You can't. That is not the way that random access files work. That is not how files work in general.

If you want to insert characters into a file you need to rewrite the file. The safe way is to do this by writing a new file with a temporary name, containing the updated contents, then delete the old one and rename the temporary file.


I know it is possible to shift but I 'know how the logic.

Firstly, "shifting" involves overwriting the characters after the insertion point.

Secondly, "shifting" is a dangerous operation. If the application is interrupted, or the system fails / is powered off while you are in the middle of the "shift" operation, you are liable to corrupt the file.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

This is the same answer as in C: you can't, the filesystem doesn't allow that. see: C function to insert text at particular location in file without over-writing the existing text

Community
  • 1
  • 1
pdem
  • 3,880
  • 1
  • 24
  • 38