Reading all lines in a file using BufferedReader
BufferedReader
can be used to read contents of a file or an input stream. It presaves some contents it reads, so the read operation is faster. In this recipe, we will learn how to read all the contents of a file in one go using BufferedReader
.
Getting ready
You need to install a preferred development environment that compiles and runs Kotlin. You can also use the command line for this purpose, for which you need the Kotlin compiler installed along with JDK. You can also use IntelliJ IDEA for the development environment.
How to do it…
In the following steps, we will learn how to use BufferedReader
to read all lines of a file:
- Let's start with getting the
InputStream
of our file and use theBufferedReader
on it to read the contents of the file in one go:
import java.io.File import java.io.InputStream fun main(args: Array<String>) { val inputStream: InputStream = File("lorem.txt").inputStream() val inputString = inputStream.bufferedReader...