Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read All Lines of BufferedReader in Scala into a String

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.

like image 572
Kevin Meredith Avatar asked Sep 20 '13 18:09

Kevin Meredith


People also ask

How do you read multiple lines in a buffer reader?

Using BufferedReader Class Another way to read multiple lines from the console can be done using the synchronized BufferedReader class in Java. The idea is to read each line using the readLine() method and use String. split() to split the line into individual tokens using whitespace as a delimiter.

Which method is for reading string in BufferedReader class?

Java BufferedReader class is used to read the text from a character-based input stream. It can be used to read data line by line by readLine() method.

What is Br readLine?

Java BufferedReader readLine() Method The readLine() method of Java BufferedReader class reads a line of text. The "/n" and "/r" are line termination character which is used to consider a line.


1 Answers

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") 
like image 66
joescii Avatar answered Sep 19 '22 09:09

joescii