Hi,

> On 2 Oct 2017, at 13:07, Dirk Olmes <d...@xanthippe.ping.de> wrote:
> 
> Hi,
> 
> I'm trying to get started with Pharo doing something really simple - at
> least that's what I thought ... I'm trying to read a text file line by line.
> 
> If I use  File named: '/tmp/linex.txt' readStream nextLine I'll get a
> debugger telling me that BinaryFileStream does not understand nextLine.
> 
> Now I've tried my best to find a stream that may be reading plain text
> lines but to no avail ...
> 
> Help!
> 
> -dirk

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

(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

Reply via email to