Today's Smalltalk Daily is a basic introduction to file handling in Smalltalk, looking at class Filename, and at basic Stream protocol. Here's the code I used in the short examples in the screencast:
"Filename is your entry point into file handling"
(Filename named: '.') directoryContents.
"Read line by line"
list := List new.
stream := 'lines.txt' asFilename readStream.
[stream atEnd]
whileFalse: [list add: stream nextLine].
stream close.
"more manually, to show stream protocol"
list := List new.
stream := 'lines.txt' asFilename readStream.
[[stream atEnd]
whileFalse: [| next|
next := stream upTo: Character cr.
list add: next]]
ensure: [stream close].
To watch, click on the viewer below:
If you have trouble viewing that directly, you can click here to download the video directly
You can also watch it on YouTube:
Technorati Tags:
smalltalk, file, stream, i/o