Programmer Reference : Common File System : Using file streams : Reading and writing file streams
Reading and writing file streams
File streams have the same protocol as CLDT Streams. For the complete set of methods supported by the Stream class, see "Stream classes". The following example uses a CfsReadFileStream to read data from an existing file, and a CfsWriteFileStream to write the data to a newly created file.
| oldData newData |
(oldData := CfsReadFileStream open: 'oldData.txt') isCfsError
ifTrue: ^self error: oldData message].
(newData := CfsWriteFileStream openEmpty: 'newData.txt') isCfsError
ifTrue: [^self error: newData message].
newData nextPutAll: oldData contents.
oldData close.
newData close.
The indexing methods for file streams answer and accept the byte position in the file, not the character position. In contrast, the indexing operations of CLDT Streams answer and accept character positions in the buffer being streamed over. When file streams are used with single-byte characters exclusively, the character position and byte positions are the same. However, this is not the case when a mixture of single and double-byte characters are used.
When used with file streams, the copyFrom:to:, position, position:, size, and skip: messages operate based upon byte indexes and sizes, not character and sizes. The other file stream operations operate in the same manner as the CLDT Stream protocols. For portability, use the next and next: protocols for stream positioning.
Last modified date: 07/16/2018