Quote:
>I drafted a method for adding text into a file stream directly. Rather than
>taking the data from the file stream, placing it in an array, then amending
>it and placing it back in it's file stream. The method ( to my own
>surprise ), actually worked,
>but it places the new text over the first line
>of the file as it was before, not much use.
>Although knowing the answer may prove quite useless, I am curious.
>How do I control the placing / positioning of text into a file stream,
>without having an array in the middle of the process ?
Well, if you want to glue more stuff at the _end_ of a file, try
opening for append (check class methods for File); or use the
positioning msgs in the other posts to ensure that your
new text will grow at the file's end instead of overwriting
characters at the beginning.
If you want to insert arbitrary strings into the beginning of the
file, for example where this...
---myfile.dat---
oldLineA
oldLineB
----------------
becomes this...
---myfile.dat---
newLineA
oldLineA
oldLineB
----------------
then I think you will, one way or another, need to
completely recreate the file.
The easiest way is, like you mentioned, slurping the
original into an Array (though an OrderedCollection
would probably be easier to manipulate), tweak the
collection, then dump it back out.
cons to this: large files = big memory requirements, and if
somebody kicks your power cord out of the wall during the
write phase, only part of your data might make it to disk
(while the rest of it evaporates from ram).
A marginally better approach would be smth like this:
addTo: aFile for: aString
| in out character |
in := File pathName: aFile.
out := File pathName: 'temp.dat'.
out nextPutAll: aString; cr.
[ (character := in next) notNil ] whileTrue: [ out nextPut: character ].
in close.
out close.
File delete: aFile.
File rename: 'temp.dat' to: aFile.
There are certainly better ways to do this. For example, Files
in your environment probably know how to add all of themselves
to another file (similar to the #addAll: msg that collections
know about). And I'd be surprised if I have the methods for
killing & moving files around correct for your environment.
But I would expect that to be some class methods somewhere
that do that sort of thing.
So the short answer woudl be it is easier if you can glue
the new strings onto the end of an existing file rather than
shoehorning them in at the beginning.
fwiw - as I recall, raw text files work like this back in the
procedural land. This is one of the reasons that writing
a good text editor gets to be non-trivial when you start
working with large amounts of text.
Quote:
>Class method for adding data to a file. Class is "Food", ( not that it
>matters ).
>addTo: aFile for: aString
> | outputStream |
> outputStream := File pathName: aFile.
> outputStream nextPutAll: aString ; cr.
> outputStream close.
>I live in hope that I start to understand this language, but right now I
>wish the information could be uploaded to my brain like in "The Matrix".
>Thanks in anticipation.
>Kelly Tait.
You're doing exactly what you need to do in order to learn this.
Keep at it; I applaud your research!
John G.