Contents

Class FileStream


Inheritance:

Object  »
  Stream  »
    PositionableStream  »
      WriteStream  »
        ReadWriteStream  »
          FileStream

I represent a Stream that accesses a FilePage from a File. One use for my instance is to access larger "virtual Strings" than can be stored contiguously in main memory. I restrict the objects stored and retrieved to be integers or Characters. An end of file pointer terminates reading; it can be extended by writing past it, or the file can be explicitly truncated.

To use the file system for most applications, you typically create a FileStream. This is done by sending a message to a FileDirectory (file:, oldFile:, newFile:, rename:newName:) which creates an instance of me. Accesses to the file are then done via my instance.

FileStream itself is now an abstract class. The class methods of FileStream do not create an instance of FileStream, but an instance of an appropriately chosen subclass of FileStream.

Subclasses of FileStream are:

*** On DOS, files cannot be shortened! *** To overwrite a file with a shorter one, first delete the old file (FileDirectory deleteFilePath: 'Hard Disk:aFolder:dataFolder:foo') or (aFileDirectory deleteFileNamed: 'foo'). Then write your new shorter version.

Examples of Use:

 | fs |
fs := FileStream fileNamed: 'squeak.ini'.

Here, fs is a StandardFileStream. The file is searched and opened for read and write access in the current working directory.

A file stream should always be closed after use:

 | fs |
fs := FileStream fileNamed: 'squeak.ini'.
fs close.

in the following example the contents of a text file are fetched, the file stream is closed and the fetched text is displayed in a workspace:

 | fs txt |

fs := FileStream fileNamed: 'squeak.ini'.
txt := fs contents.
fs close.

Workspace new
    contents: txt;
    openLabel: 'Contents of squeak.ini'

Text files have a line structure. The following example shows how to count the number of lines in a text file:

 | fs nroOfLines  |
fs := FileStream fileNamed: 'squeak.ini'.

nroOfLines := 0.

[fs atEnd]
   whileFalse:
     [fs nextLine.
	 nroOfLines := nroOfLines + 1.
     ].

fs close.
nroOfLines 
13

Contents