Bober02
Bober02

Reputation: 15341

scala - lazy file/url iterator

As far as I know iterators of files/urls in scala are lazy, i.e.

scala.io.Source.fromFile("c:/tmp.csv") getLines()

should return an Iterator[String] which has not yet read the file in and is simply pointing to the first line of that file. yet, if I debug this code, stop on the next line, go and physically change the file on the HDD, the values returned by this iterator correspond to the file from before the update. Why is that the case?

This is what I would epect from a Java iterator, that would prefetch the wholefile into the memory

Upvotes: 1

Views: 361

Answers (1)

Rogach
Rogach

Reputation: 27200

Obviously, reads from the file are buffered (to increase performance, since disk access is slow). So when you start reading from a file, some part of it is read into buffer right away (for example, 4kb), so when you edit that already-read part, it doesn't change in your program.

I tried to do this with 7Mb file - I opened the file, edited the last line and the edit was properly reflected in the code. On the contary, when I did the same trick with 4Kb file, I got the behavior you describe.

EDIT: I suspect that the actual buffering happens somewhere around these lines (I love the comments there :) ).

EDIT2: Actually, I feel that you found some funny bug - since I'm looking at source for half an hour now, and I still don't see the place where the buffering could take place before the call to getLines.

Upvotes: 1

Related Questions