About getting lines

$ cat > /tmp/lines.txt
one
two
three

Here are different ways to get lines
(FileLocator temp / ‘lines.txt’) contents lines.

‘/tmp/lines.txt’ asFileReference contents lines.

‘/tmp/lines.txt’ asFileReference readStreamDo: [ :in |
Array streamContents: [ :out |
[ in atEnd ] whileFalse: [ out nextPut: in nextLine ] ] ].

(File named: ‘/tmp/lines.txt’) readStreamDo: [ :in |
| characterStream |
characterStream := ZnCharacterReadStream on: in.
Array streamContents: [ :out |
[ characterStream atEnd ] whileFalse: [ out nextPut: characterStream nextLine ] ] ].

They all return #(‘one’ ‘two’ ‘three’).

In the last, more complex example, you first get a binary stream (and a ‘line’ is a character based concept), so wrapping the binary stream in a character read stream (which does know about lines) solves the problem.

HTH,

Sven

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: