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.
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.
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.
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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With