Read File Content as List of Lines in Kotlin

To read file content as List of lines, use File.readLines() method.

In this tutorial, we will learn how to read file content as a list of lines in Kotlin.

The function used to read the lines from a text file is

File.readLines()Reads file content as a list of lines

Example 1 – Read Lines in Text file as List

In this example, we will read a text file, “file.txt”, present at the root of the project. We will use File.readLines() method.


Hello World.
Welcome to Kotlin Tutorial by
Learning Kotlin is easy.



 * Kotlin Example to read contents of file line by line
fun main(args: Array) {
    val fileName :String = "file.txt"
    var i :Int = 1
    // using extension function readLines
    File(fileName).readLines().forEach {
        println(": "+it)


1: Hello World.
2: Welcome to Kotlin Tutorial by
3: Learning Kotlin is easy.


In this Kotlin Tutorial – Kotlin Read File to Lines, we have learnt to read content of file line by line. In our next tutorial, we shall learn to write data to file.