On Aug 19, 10:47 am, Eric Sosman <Eric.Sos...@sun.com> wrote:
Danger_Duck wrote:
So I need to insert a string at the top of a file. PrintWriter has no
such insertion method, nor Writer, nor FileWriter.
Do you know of any way to insert text into a file? The only thing I
can think of is to copy all the text, append it to the inserted text,
delete and recreate the file.
This of course takes both coding time and running time, and I would
imagine that there must be a simple way to add to the contents of a
text file...
Oh, drat! You forgot "Adelia," which belongs at the top of the
list -- but since you knew Donizetti was prolific and the list would
be long, you started right at the top edge of the paper and there's
no space above the existing first entry. Now ponder what sort of
"simple way" would allow you to insert "Adelia" in its proper place
without recopying.
Heh, ok. I was thinking that the file was stored as an array of
characters rather than a piece of paper though, and there might be
some way to move the pointer that points to the first element of the
array back by the number of characters I have to prepend. Then I could
copy the characters in and all would be well.
Of course, I wouldn't have to do such a thing manually, but instead
use the method File.prepend(String s)....alas, one can dream :(
Let us say that your file system uses disk blocks of 4096 bytes.
And you need to insert something at the beginning. If what you insert
the new blocks. But if it is not a multipla of 40956, then it can not
be done for the same reasons as the piece of paper.
You simply can not do it.