“close” a Stream?

后端 未结 1 1520

I am reading text from a java BufferedReader like this:

Stream.continually(reader.readLine).takeWhile { 
   case null => reader.close; false         


        
相关标签:
1条回答
  • 2021-01-18 18:11

    You can get the .whenDone behaviour by appending another Stream. This makes the code a bit more expressive and could also be used in other cases. It is something but I guess far from perfect.

    def closeStream: Stream[Nothing] = {
      reader.close
      Stream.Empty
    }
    
    Stream.continually(reader.readLine).takeWhile(_ != null) #::: closeStream
    
    0 讨论(0)
提交回复
热议问题