saving / loading simple objects

Eric Scharff Eric.Scharff at Colorado.EDU
Tue Mar 20 02:06:46 UTC 2001


I have a simple question, although I'm not sure how simple the answer is.
I tried to get the answer from browsing around and failed... :(

I want to have some persistent storage of some very simple objects that I
have created.  I say that the objects are "simple", meaning they are
either objects whose member variables are simple types (strings, numbers,
etc.) or are collections of simple objects.

For example, right now I just have an object SimpleComment with two
instance variables, a date (Date) and a comment (String).  I have an
OrderedCollection of SimpleComments I want to read and write from disk to
have some backup mechanism I understand. :)

Human readability, XML, databases, and the like aren't necessary (yet),
but may be nice in the future.

There seems to be two ways to do this.  The first is to use storeOn:,
which would look something like this:

 | outFile |
 "allComments is an OrderedColleciton of SimpleComments"
 outFile _ (FileStream fileNamed: 'simple.st').
 allComments storeOn: outFile.
 outFile close.

I could read this back in, but doing so felt awkward:

 | inFile |
 inFile _ (FileStream fileNamed: 'simple.st').
 allComments _ Compiler evaluate: (inFile contentsOfEntireFile).
 inFile close.

The other technique I ran across used the SmartReferenceStream.  It works
like this.

 allComments saveOnFile. "Prompts user for file name"

But, again, reading this back in seems rather weird:

 | inFile |
 inFile _ (FileStream fileNamed: 'simple2.st').
 allComments _ inFile fileInObjectAndCode.
 inFile close.

Is there a "right" way to do this?  Is there some better technique for
this problem that I should probably adopt?

-Eric






More information about the Squeak-dev mailing list