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 write text and image files

Team Robot Chicken

How to write text and image files

Uploaded Image: RobotChicken.jpg
by: Amro Mousa, Elliott Clark, Brandon Reynolds, Jiasheng He


Writing text files:

Writing text files is actually quite simple. This has a variety of uses such as writing HTML pages for use with Comanche, etc. The code below simply creates a text file named sample.txt in the squeak directory (hence the default in dir := FileDirectory default.) with the text "This is the text file I wanted to write." I've numbered each line below for explanation:

Code:
1) | dir filename text |
2) text := 'This is the text file I wanted to write'.
3) filename := 'sample.txt'.
4) dir := FileDirectory default.
5) (dir fileExists: fileName) ifTrue:[ dir deleteFileNamed: fileName ].
6) filename isEmpty ifTrue:[^self].
7) file := dir newFileNamed: fileName.
8) file nextPutAll: text.
9) file close.


Explanation by line number:
1) Setup temporary variables
2) Set the object named text to the text we want in the text file
3) Set the filename to the string value we want it to have
4) Set the directory to the default directory (the Squeak folder)
5) Check if the file exists in the directory, if so, delete it before writing
6) Check if the filename is empty (i.e. no actual name given), if so return without writing
7) Create a new file in the specified directory named "sample.txt"
8) Write the text to the file
9) Close the file

Writing image files:

Writing image files is just as easy as writing text files. Again, Squeak has the functionality built in, we just need to use it. The specific example given below is for writing Gif images. There are facilities in Squeak for writing out other popular image formats, which will be covered later.

Code:
1) | fileName |
2) fileName := 'slide.gif'.
3) FileDirectory default deleteFileNamed: fileName ifAbsent: [].
4) fileName isEmpty ifTrue:[^self].
5) GIFReadWriter putForm: self imageForm onFileNamed: fileName.

Explanation by line number:
1) Setup temporary variables
2) Set the file name to "slide.gif"
3) Another way to check if a file exists, this time assuming it is there and doing nothing if it isn't
4) Check if the filename is empty (i.e. no actual name given), if so return without writing
5) Get the form of the current Morph (self imageForm) and write it to the file named "slide.gif"

Above, you'll notice we used a GifReadWriter to write standard Gif images. It is also possible, using similar methods, to write to bitmaps, jpegs, pcx, and png files. To accomplish this you should use BMPReadWriter, JPEGReadWriter, PCXReadWriter, and PNGReadWriter respectively.

As always, these code snippets can (and should) be used in methods. Replace the static file names with passed in variables or numbers generated in a loop, etc. and you'll be set.

Link to this Page