2010-07-22 58 views
6

我試圖從創建的readLine串無限流呼籲:流的readlines方法

import java.io.{BufferedReader, InputStreamReader} 
val in = new BufferedReader(new InputStreamReader(System in)) 
val input: Stream[String] = Stream.cons(in readLine, input) 

但看來,readline的通話不會被調用懶洋洋地。輸入該代碼後,readLine立即輸入,然後Stream成爲該輸入的無限列表。是否有可能完成我的想法?

回答

11
import java.io.{BufferedReader, InputStreamReader} 
val in = new BufferedReader(new InputStreamReader(System in)) 
val input = Stream.continually(in readLine) 
+0

您是否需要關閉流? – Programmer 2017-10-29 06:20:50