File Reading in Smalltalk: Video
Today's screencast 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 - The #nextLine message is part of the NetClients package - when I did the recording, I didn't notice that. To get that code, load the NetClients parcel:
"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].
You can download the video directly here. If you like this kind of video, why not subscribe to "Smalltalk Daily"?