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

Saving and Reading Text Files for Dummies

Saving and Reading Text Files for Dummies

In our project we had to make a logging class which would record activities of the program into the text files. I was not able to find any information about the issue on any cases or lecture slides, which is why I think some of the future teams may find it useful.

Creating text files is a very simple process and I will keep it as simple as I can.

Creating a class which will record the text data.

1. Create a regular empty class and in it’s initialize method add the following line:

stream := 'logger.txt' asFilename readWriteStream.

In this case the variable “stream” is an instance variable and the ‘logger.txt’ is the file name.

2. Then you need to create a method which will be used by another class to add string objects to the text file:
add: aString
"adds a string object to the log"
    stream nextPutAll: aString.
    stream cr.
    stream close.

More information about the code

1. When you use asFilename, it will create a Filename object. Filename class operates differently under other operating systems, which is why it is portable. In windows OS ‘logger.txt’ asFilename creates a Filename object, which targets the image directory in Visual Works (i.e. “C:\Program Files\Cincom\vw7.5nc\image”).

In order to read or save a file streams must be used.
The readWriteStream was used in my example. It only creates one file and allows reading and writing to it. Although I never actually read from the log file, I would not be able to use writeStream because it always creates a new file every time I call that method. It prohibits editing, and only used when something needs to be saved once. For example, writeStream could be used to make a word processor like MS Notepad. Another kind if steam is readStream which only lets the programmer read the certain file and it does not allow any changing of the original file.

2. Steam package can be found under “Base VisualWorks / Collections / Collections-Streams”. The most widely used methods for read and write streams are:
      nextPutAll: aString - This method adds a string object to the file.
     cr – this method adds a new character line to the text file.
     tab – this method adds a tab character to the text file.
     space – this method adds a space character to the text file.
     close – the stream needs to be closed when the writing to the text file is complete.

If you are making a logger, you can find useful this line:
stream nextPutAll: ' ', Time dateAndTimeNow printString, ' ', aString.

It simply adds the time in readable format in front of the event, so that it is easier to track the logging.

Reading Files

Smalltalk makes reading files very easy. Take a look at the following code:

stream := 'logger.txt' asFilename readStream.
    [stream atEnd] whileFalse:[nextline := stream nextLine].
    stream close.


The first line of the codes opens a file named “logger.txt” from the visual image directory. Stream atEnd tests the position for the End Of File character and True when it reaches it.

The method nextLine obviously takes the next line of the text file and returns as a String object.

Links to this Page