将Scala中的BufferedReader的所有行读入字符串 [英] Read All Lines of BufferedReader in Scala into a String

查看:377
本文介绍了将Scala中的BufferedReader的所有行读入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何读取 BufferedReader 行的全部并存储到字符串中?

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

 val br = new BufferedReader(...)
 val str: String = getAllLines(br) // getAllLines() -- is where I need help

类似于此

Similar to this question.

推荐答案

这是我在Scala中处理 BufferedReader 的方式:

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")

这篇关于将Scala中的BufferedReader的所有行读入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆