=== Top of the Swiki === Attachments ===

Recipe: Reading a file

Problem

You want to read in a text file to the Squeak environment.


Solution

>
You create an instance of a FileStream on the file.

file := FileStream fileNamed: 'test.txt'.

Then you can do things like...

Process a line at a time:

[file atEnd] whileFalse: [
line := file nextLine.
"Process the line"
]


Or just read the whole file into a String:

string := file upToEnd.

Discussion


Look at the Stream protocol, particularily in Stream and
PositionableStream.

If you just need to read the file, open FileList and it will open in a
Workspace.

See also





Questions