Best way to use BufferedReader in Kotlin

So I just started using Kotlin for Android and converted my Android codes to Kotlin.

In one of the conversions, I came across a BufferedReader, which I usually write in Java as follows:

String result = ""; String line = ""; BufferedReader reader = new BufferedReader(someStream); while ( (line = reader.readLine()) != null ) { result += line; } 

But in Kotlin it seems that Kotlin does not allow me to assign values ​​to variables in conditions.

Currently, I have written code as follows:

 val reader = BufferedReader(someStream) var line : String? = "" while (line != null) { line = reader.readLine() result += line } 

which I do not find so elegant and a feeling of foreboding, despite the use of Kotlin.

What would be the best way to use BufferedReader in Kotlin?

+19
source share
6 answers

You can use bufferedReader so

 val allText = inputStream.bufferedReader().use(BufferedReader::readText) 
+46
source

If you still want to read it one at a time, you can use some extension functions from std lib and do it like this:

 val reader = someStream.bufferedReader() val iterator = reader.linesSequences().iterator() while(iterator.hasNext()) { val line = iterator.next() // do something with line... } reader.close() 

or alternatively using a “functional” approach:

 val reader = someStream.bufferedReader() reader.useLines { it.map { line -> // do something with line } } 

using useLines, you do not need to explicitly close the reader, the extensionLines extensions function will do it for you!

Just add them for reference .. cheers

+8
source

Thanks to the link of Juan Gonçalves to stdlib, I discovered that you can use forEachLine to bypass the reader if necessary.

+1
source

use this code

     val input = conn.inputStream
     val allText = input.bufferedReader (). use (BufferedReader :: readText)
     val result = StringBuilder ()                   

     result.append (allText)
     return result.toString ()

     } else {

     return "unsuccessful"

     }

0
source

Another way is to use a for loop:

 val reader = BufferedReader(someStream) for (line in reader.lines()) { println(line) } 

Although this is not as short as the accepted answer, it will allow you to execute a loop and execute some kind of logic without accumulating everything in one line, as shown below

 val allText: String = inputStream.bufferedReader().use(BufferedReader::readText) 
0
source

You can also try using the "forEachLine" method.

 val file = File("./folder/test.txt") file.bufferedReader().forEachLine { println("value = $it") } 

it will also automatically close the stream after reading the last line

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/java.io.-reader/for-each-line.html

fun Reader.forEachLine (action: (String) -> Unit)
Iterates over each line of this reader, invokes an action for each read line, and closes the reader after it is completed.

0
source

Source: https://habr.com/ru/post/1012954/


All Articles