Read All Lines of BufferedReader in Scala into a String

前端 未结 1 857
轮回少年
轮回少年 2021-02-02 09:33

How can I read all of a BufferedReader\'s lines and store into a String?

 val br = new BufferedReader(...)
 val str: String = getAl         


        
相关标签:
1条回答
  • 2021-02-02 10:19

    This is how I deal with a BufferedReader in Scala:

    val br:BufferedReader = ???
    val strs = Stream.continually(br.readLine()).takeWhile(_ != null)
    

    You will have a string for each line from the reader. If you want it in one single string:

    val str = Stream.continually(br.readLine()).takeWhile(_ != null).mkString("\n")
    
    0 讨论(0)
提交回复
热议问题