View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

How to implement save and load functions without using BOSS

For the final projects, it is required to implement a function to save and load.
VisualNC provides something called BOSS that lets you save and load.
However, there is a way to do the same function without using the BOSS:
you can simply save all the data you currently have into a file in a text file format.


To do this, first of all you will need to know how to manipulate a file using filestream.
You can open a file stream this way in smalltalk:

| stream1 stream2 |
stream1 := 'aFilename.txt' asFilename writeStream. "Write stream to save into file."
stream2 := 'aFilename.txt' asFilename readStream. "Read stream to load from file."
stream1 close. "Close the filestream"
stream2 close. "Close the filestream"

Once you have opened a stream, you can write to or read from the file.

To write:
stream1 nextPutAll: 'aString'. "Writes a string into file."
stream1 nextPut: $,. "Writes a character, in this case a comma into file."
stream1 nextPut: Character tab. "Writes a tab into file."

To read:
| temp |
temp := stream2 upTo: Character tab. "Reads string up to tab from file."
temp := stream2 upTo: $,. "Reads string up to a comma from file."


Using just these few commands, you can implement functions to save and load.
A very simple exmple to save and load an instance of Inventory class storing
multiple instances of Item class.

To save:
| inv stream |
inv := Inventory new.
inv addItem: (Item named: 'PS3' quantity: 50 buyPrice: 399.99).
inv addItem: (Item named: 'XBox' quantity: 30 buyPrice: 299.99).
inv addItem: (Item named: 'Wii' quantity: 40 buyPrice: 199.99).


stream := 'c:\aFilename.txt' asFilename writeStream.

(inv items) do: [:e |
stream nextPutAll: e name.
stream nextPut: $,.
stream nextPutAll: e quantity printString.
stream nextPut: $,.
stream nextPutAll: e buyPrice printString.
stream nextPut: $,.
].

stream close.

To load:
| inv stream name quantity price |
inv := Inventory new.

stream := 'c:\aFilename.txt' asFilename readStream.

[stream atEnd] whileFalse: [
name := stream upTo: $,.
quantity := (stream upTo: $,) asNumber.
price := (stream upTo: $,) asNumber.

inv addItem: (Item named: name quantity: quantity buyPrice: price).

].

stream close.


After these saving and loading, we can check to see if it works correctly.

Test code:
(inv items) do: [:e |
Transcript show: e printString, ' - '.
].

Output:
50, PS3, $399.99 - 40, Wii, $199.99 - 30, XBox, $299.9950 -


In this individual case, my only intention was to simply show you how to use
file stream functions to load and save data. Therefore, when applying this to
your project, you will need to consider more than just wahat I presented you.



Using BOSS provided by the VisualNC could be easier way to implement save and
load functions; however, by doing it this way you simple save the data into
a file as text form, which means as long as you follow the same parsing rules,
you can load the saved data using any other programming languages.



Link to this Page