January 2018
Intermediate to advanced
434 pages
14h 1m
English
We can also use other methods like readLine() for this purpose. This code is an example of that:
import java.io.Fileimport java.io.InputStreamfun main(args: Array<String>) { val listOfLines = mutableListOf<String>() val reader = File("lorem.txt").bufferedReader() while(true) { var line = reader.readLine() if(line == null) break listOfLines.add("> "+line) } listOfLines.forEach{println(it)}}
The great thing about using the useLines() method is that it closes the stream post-execution. Also, the code in the preceding examples was a more idiomatic and clean way of doing the same thing.
Another method provided by Kotlin that returns a sequence of lines is lineSequence(), but it does not close the BufferedReader after execution, which ...
Read now
Unlock full access