Next: , Previous: Namespaces, Up: Features

2.4 Disk file-IO primitive messages

Four classes (FileDescriptor, FileStream, File, Directory) allow you to create files and access the file system in a fully object-oriented way.

FileDescriptor and FileStream are much more powerful than the corresponding C language facilities (the difference between the two is that, like the C stdio library, FileStream does buffering). For one thing, they allow you to write raw binary data in a portable endian-neutral format. But, more importantly, these classes transparently implement virtual filesystems and asynchronous I/O.

Asynchronous I/O means that an input/output operation blocks the Smalltalk Process that is doing it, but not the others, which makes them very useful in the context of network programming. Virtual file systems mean that these objects can transparently extract files from archives such as tar and gzip files, through a mechanism that can be extended through either shell scripting or Smalltalk programming. For more information on these classes, look in the class reference, under the VFS namespace. URLs may be used as file names; though, unless you have loaded the NetClients package (see Network support), only file URLs will be accepted.

In addition, the three files, stdin, stdout, and stderr are declared as global instances of FileStream that are bound to the proper values as passed to the C virtual machine. They can be accessed as either stdout and FileStream stdout—the former is easier to type, but the latter can be clearer.

Finally, Object defines four other methods: print and printNl, store and storeNl. These do a printOn: or storeOn: to the “Transcript” object; this object, which is the sole instance of class TextCollector, normally delegates write operations to stdout. If you load the Blox gui, instead, the Transcript Window will be attached to the Transcript object (see Blox).

The fileIn: message sent to the FileStream class, with a file name as a string argument, will cause that file to be loaded into Smalltalk.

For example,

     FileStream fileIn: '' !

will cause to be loaded into gnu Smalltalk.