29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

external device (including a file) then it essentially acts as <strong>an</strong> interface to that external device. It thus<br />

allows messages to be sent to <strong><strong>an</strong>d</strong> received from <strong>an</strong> external device object enabling it to accomplish<br />

various activities including input <strong><strong>an</strong>d</strong> output. If a stream is connected to a collection (such as a string) it<br />

c<strong>an</strong> act as a me<strong>an</strong>s for processing the contents of a collection. In both cases the stream views the source<br />

(or sink) of the data as being able to provide (or receive) a data item on request.<br />

13.3 ASCII files<br />

13.3.1 Working with ASCII files<br />

There are a wide r<strong>an</strong>ge of file h<strong><strong>an</strong>d</strong>ling facilities available within <strong>Smalltalk</strong>. Most are provided by one<br />

of the subclasses of the class Filename <strong><strong>an</strong>d</strong> Stream . The Filename class is <strong>an</strong> abstract superclass in<br />

which the basic framework for files is defined. Most of the work of this class is actually performed by<br />

one of its (platform specific) subclasses. It is this which is one of the facilities which allows you to<br />

ensure that your code c<strong>an</strong> run on <strong>an</strong>y platform, even if you are accessing the host platform’s file system!<br />

In the remainder of this section we shall look at some of the most common operations.<br />

13.3.2 Creating, reading <strong><strong>an</strong>d</strong> writing a file<br />

To create a file you must first have a filename to work with. A filename is created by sending the<br />

message asFilename to a string. This converts the string into <strong>an</strong> appropriate form for the host operating<br />

system. You now have a filename, however you need to link this file name with <strong>an</strong> actual file. To do<br />

this you use <strong>an</strong> inst<strong>an</strong>ce of one of the streams classes (see earlier in chapter). This c<strong>an</strong> be done by<br />

sending the message writeStream, readStream or readWriteStream to a filename (it is rarely a good idea<br />

to use the readWriteStream option. It is invariably better to use either the readStream or writeStream<br />

messages). In effect this links the resulting inst<strong>an</strong>ce directly to the actual file. In the case of creating a<br />

new file, it also creates the file for you when the first character is written to it. You now have something<br />

to which you c<strong>an</strong> send ASCII text.<br />

You may wish to check to see if a file exists before attempting to open it (e.g. to be sure there is<br />

something to read from or to make sure you don’t overwrite <strong>an</strong> existing file). You c<strong>an</strong> do this with the<br />

exists message. This message returns true if the file is already present <strong><strong>an</strong>d</strong> false if it does not exist.<br />

To close the file once it has been opened there is a close message. This message closes the<br />

associated file as well as the data stream to that file.<br />

Try typing the following into the Workspace <strong><strong>an</strong>d</strong> evaluating it.<br />

| filename stream |<br />

filename := 'temp.txt' asFilename.<br />

(fileName exists)<br />

ifTrue:<br />

[Dialog warn: 'file ' , fileName asString, ' already exists'.]<br />

ifFalse: [<br />

stream := fileName writeStream.<br />

stream nextPutAll: 'Hello World'.<br />

stream close.].<br />

Tr<strong>an</strong>script show: fileName fileSize printString.<br />

This example creates a file called “temp.txt” in the current working directory, assuming that one<br />

does not already exist. It then puts the string “Hello World” in that file <strong><strong>an</strong>d</strong> closes the file. It then prints<br />

the size of the file in the system Tr<strong>an</strong>script. For example, using VisualWorks 2.5 on a Windows -95 PC,<br />

this results in the value 11 being printed in the Tr<strong>an</strong>script. If the code is evaluated a second time, it<br />

results in a dial og box (illustrated in Figure 13.2) being displayed which warns that the temp.txt file<br />

already exists.<br />

Figure 13.2: A file exists dialog<br />

112

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!