Just use Source.fromFile(...).getLines
as you already stated.
That returns an Iterator, which is already lazy (You'd use stream as a lazy collection where you wanted previously retrieved values to be memoized, so you can read them again)
If you're getting memory problems, then the problem will lie in what you're doing after getLines. Any operation like toList
, which forces a strict collection, will cause the problem.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…