Re: Do any java.io classes support inserting text into a file?
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...
In four decades' experience with a reasonably wide variety of
computing environments, I've only encountered *one* system whose
text files supported this operation.
YMMV.
By the way, you might find it interesting to try this simple
exercise: Take a piece of paper and make a list of all the operas
of Gaetano Donizetti, in alphabetical order by title. You begin
with "Alahor in Granata" and continue through "Zoraida di Granata"
(Spain seems to have been a popular locale for opera plots). Then
you count the lines and compare the total with the count from some
reference book -- hey, something's wrong, you seem to be one short.
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.
(The one system I mentioned solved the problem, essentially, by
taping a slip of blank paper to the top of the written page and then
writing "Adelia" on it. Oh, and subsequent readers of the list had
to be taught to ignore the extra vertical white space that would
inevitably appear.)
--
Eric.Sosman@sun.com