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.

Notice that in this example we used the message nextPutAll: to save the single string ‘Hello<br />

World’. Remember that strings are actually collections <strong><strong>an</strong>d</strong> what this says is save the whole contents of<br />

this collection to the file.<br />

An import<strong>an</strong>t point to note about files is that it may sometimes be necessary to access a file which is<br />

in a different directory to the working directory. This c<strong>an</strong> be a cause of problems when porting between<br />

platforms unless care is taken. If you hard code the name of a directory structure into your system then<br />

you will need to modify the system code before it will work on a different platform. There is a way<br />

around this using the construct: message. This constructs a path name which is appropriate for the<br />

platform on which you are working. For example:<br />

(('dcs' asFilename construct: 'jjh')<br />

construct: 'visual' )<br />

construct: 'examples'.<br />

Try typing the above into a Workspace <strong><strong>an</strong>d</strong> inspecting the result. The actual pathname you obtain will<br />

depend on your host operating system. For example, on a Windows -95 PC you shou ld see the style of<br />

pathname illustrated in Figure 13.3.<br />

Other messages which may be of use include:<br />

Figure 13.3: Inspecting a filename on a PC<br />

• directory Return the directory containing the receiving file.<br />

• isDirectory Check to see if a filename is actually a directory.<br />

It is also possible to obtain information on the date <strong><strong>an</strong>d</strong> time a file was modified, accessed or had its<br />

status ch<strong>an</strong>ged. To do this first use the message dates (this is <strong>an</strong>other aspect which c<strong>an</strong> differ from<br />

one <strong>Smalltalk</strong> implementation to <strong>an</strong>other). This is sent to a filename, which returns a dictionary of<br />

associations. Then the key #modified will access the most recent modification, the key #accessed<br />

will return the last access date <strong><strong>an</strong>d</strong> the key #statusCh<strong>an</strong>ged will provide the date of the most recent<br />

ch<strong>an</strong>ge in the files status (or privileges). For example Figure 13.4 illustrates checking when a file was<br />

last accessed, modified <strong><strong>an</strong>d</strong> its status ch<strong>an</strong>ged.<br />

Figure 13.4: Accessing a files details<br />

The result of evaluating the code in Figure 13.4 on the temp.txt file created earlier might look like:<br />

Last a ccess #(June 24, 1996 11:00:00 pm) Last modified #(June 25, 1996 1:56:54 pm)<br />

Status ch<strong>an</strong>ged nil<br />

113

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

Saved successfully!

Ooh no, something went wrong!