Kotlin:
Reading a text file

How to:

In Kotlin, you can read a text file easily using the readLines() function or the useLines block.

import java.io.File

fun main() {
    // Read all lines at once
    val lines = File("example.txt").readLines()
    lines.forEach { line ->
        println(line)
    }

    // More efficient for large files
    File("example.txt").useLines { lines ->
        lines.forEach { line ->
            println(line)
        }
    }
}

Sample output (assuming example.txt contains two lines with “Hello” and “World”):

Hello
World

Deep Dive

Historically, reading files in Java could be verbose and clunky. With Kotlin, the standard library provides handy extensions to make file reading simpler.

There are alternatives for file reading in Kotlin:

  1. readText() reads the entire file content into a String.
  2. bufferedReader() provides a BufferedReader that allows you to handle more complex use cases like reading huge files without consuming too much memory.

Implementation-wise, when you use useLines, it takes care of closing the file after execution, preventing potential memory leaks. It’s a functional approach that’s encouraged in Kotlin for managing resources effectively.

See Also