1. Using BufferedReader

    import java.io.File
    import java.io.BufferedReader

    fun main(args: Array<String>) {
        val bufferedReader: BufferedReader = File("example.txt").bufferedReader()    
        val inputString = bufferedReader.use { it.readText() }
        println(inputString)
    }

2. Using InputStream

Read By Line

    import java.io.File
    import java.io.InputStream

    fun main(args: Array<String>) {
        val inputStream: InputStream = File("example.txt").inputStream()
        val lineList = mutableListOf<String>()

        inputStream.bufferedReader().forEachLine { lineList.add(it) } 
        lineList.forEach{println(">  " + it)}
    }

Read All Lines

    import java.io.File
    import java.io.InputStream

    fun main(args: Array<String>) {
        val inputStream: InputStream = File("example.txt").inputStream()
        val inputString = inputStream.bufferedReader().use { it.readText() }
        println(inputString)
    }

3. Use File directly

    import java.io.File
    import java.io.BufferedReader

    fun main(args: Array<String>) {
        val lineList = mutableListOf<String>()

        File("example.txt").useLines { lines -> lines.forEach { lineList.add(it) }}
        lineList.forEach { println(">  " + it) }
    }
Answer from Anisuzzaman Babla on Stack Overflow
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ kotlin โ€บ kotlin io โ€บ reading from a file in kotlin
Reading from a File in Kotlin | Baeldung on Kotlin
April 28, 2026 - We should pass the full path of ... path in our resources for the last two. Reads a file line by line using the specified charset (default is UTF-8) and calls action for each line:...
๐ŸŒ
ZetCode
zetcode.com โ€บ kotlin โ€บ readfile
Kotlin read file - reading file in Kotlin
January 29, 2024 - ... The Battle of Thermopylae was ... during the second Persian invasion of Greece. In the examples, we use this text file. File.readLines reads the file content as a list of lines......
๐ŸŒ
BezKoder
bezkoder.com โ€บ home โ€บ how to read file in kotlin
How to read File in Kotlin - BezKoder
February 16, 2020 - import java.io.File import ... { it.readText() } println(inputString) } ... use Reader.useLines() method with Kotlin Sequence (a sequence of all the lines) inside block....
๐ŸŒ
Techie Delight
techiedelight.com โ€บ home โ€บ java โ€บ read a file line-by-line in kotlin
Read a file line-by-line in Kotlin | Techie Delight
2 weeks ago - The standard solution to read a file line-by-line is using the BufferedReader#readLine() function. Each call to the readLine() function reads the next line from the file and returns it as a string.
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ kotlin โ€บ read-contents-of-a-file-in-kotlin
How to Read a File in Kotlin using BufferedReader or InputStream
June 19, 2017 - For most small text files, File.readText() is the shortest option. For line-by-line processing, File.forEachLine(), File.useLines(), or a BufferedReader is usually a better choice.
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ kotlin โ€บ kotlin-read-file-line-by-line
How to read a file line by line in Kotlin?
May 9, 2023 - In the following program, we read the text file info.txt line by, and print these lines to standard output. ... import java.io.File import java.io.IOException import java.util.* fun main() { val fileName = "info.txt" try { val file = File(fileName) val sc = Scanner(file) while (sc.hasNextLine()) { val line = sc.nextLine() println(line) } } catch (e: IOException) { e.printStackTrace() } } ... In this Kotlin Tutorial, we learned how to read a file line by line using java.util.Scanner class.
๐ŸŒ
Kotlin
kotlinandroid.org โ€บ kotlin android home โ€บ kotlin tutorials โ€บ kotlin file operations โ€บ kotlin โ€“ read text file line by line
Kotlin โ€“ Read Text File Line by Line - Kotlin Android
Inside the try block, create a BufferedReader by passing a FileReader that reads from the file specified by filePath. Use a while loop to read the file line by line.
Find elsewhere
๐ŸŒ
Kotlin
kotlinlang.org โ€บ api โ€บ core โ€บ kotlin-stdlib โ€บ kotlin.io.path โ€บ read-lines.html
readLines | Core API โ€“ Kotlin Programming Language
kotlin-stdlib/kotlin.io.path/readLines ยท JVM ยท inline fun Path.readLines(charset: Charset = Charsets.UTF_8): List<String>(source) Reads the file content as a list of lines. It's not recommended to use this function on huge files.
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ kotlin โ€บ reads-file-content-as-a-list-of-lines-in-kotlin-example
How to read file content as a list of lines in Kotlin - Kotlin Example
August 19, 2017 - For large files, use useLines() to process the file line by line. You can call readLines() and then use forEachIndexed to access both the index and the line text. </> Copy ยท import java.io.File fun main() { File("file.txt").readLines().for...
๐ŸŒ
Kotlin
kotlinlang.org โ€บ api โ€บ latest โ€บ jvm โ€บ stdlib โ€บ kotlin.io โ€บ java.io.-file โ€บ read-lines.html
readLines - Kotlin Programming Language
Try the revamped Kotlin docs design! ... Reads the file content as a list of lines. Do not use this function for huge files. charset - character set to use. By default uses UTF-8 charset.
๐ŸŒ
FrontBackend
frontbackend.com โ€บ kotlin โ€บ how-to-read-a-file-in-kotlin
How to read a file in Kotlin | FrontBackend
March 17, 2021 - For a sake of simplicity let's put it into a folder with our sample Kotlin files. The first approach use BufferedReader and use method: ... import java.io.File import java.io.InputStream fun main(args: Array<String>) { val inputStream: InputStream = File("frontbackend.txt").inputStream() val content = inputStream.bufferedReader().use { it.readText() } println(content) }
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ kotlin โ€บ read-from-files-using-inputreader-in-kotlin
Read From Files using InputReader in Kotlin - GeeksforGeeks
July 23, 2025 - Now, what if we want to read the file line by line because we want to do some processing on each line? In that case, we use the useLines( ) method in place of use().
๐ŸŒ
Sling Academy
slingacademy.com โ€บ article โ€บ reading-files-line-by-line-in-kotlin
Reading Files Line by Line in Kotlin - Sling Academy
November 30, 2024 - The simplest way to read a file line by line in Kotlin is by using BufferedReader.
๐ŸŒ
DhiWise
dhiwise.com โ€บ post โ€บ simplifying-kotlin-read-from-file-a-practical-approach
Kotlin Read From File: Step-by-Step Instructions Simplified
January 21, 2025 - This blog will take you through the most common techniques to read files in Kotlin, showcasing practical examples and explaining concepts like file line-by-line reading, managing input streams, and dealing with file content.
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ kotlin โ€บ kotlin โ€“ reading large files efficiently
Kotlin - Reading Large Files Efficiently - Mkyong.com
March 3, 2025 - Kotlin provides multiple ways to read large files efficiently. Letโ€™s explore them: The BufferedReader reads text from an input stream while buffering characters for efficient reading. ... import java.io.File fun main() { val file = File("largefile.txt") file.bufferedReader().use { reader -> reader.lineSequence().forEach { line -> println(line) // Process each line } } }
๐ŸŒ
Kotlin
kotlinlang.org โ€บ api โ€บ latest โ€บ jvm โ€บ stdlib โ€บ kotlin.io โ€บ read-line.html
Kotlinlang
The Kotlin Standard Library provides ... with Kotlin. These include: Higher-order functions implementing idiomatic patterns (let, apply, use, synchronized, etc). Extension functions providing querying operations for collections (eager) and sequences (lazy). Various utilities for working with strings and char sequences. Extensions for JDK classes making it convenient to work with files, IO, and ...
๐ŸŒ
Java Code Geeks
javacodegeeks.com โ€บ home โ€บ jvm languages โ€บ kotlin
Efficiently Reading Large Files in Kotlin - Java Code Geeks
April 11, 2025 - When working with large files in Kotlin, selecting the right approach is crucial: BufferedReader is best for reading large text files line by line, useLines with Sequence is ideal for the lazy processing of text files, and InputStream with Buffer ...
๐ŸŒ
Kotlin
kotlinandroid.org โ€บ kotlin android home โ€บ kotlin tutorials โ€บ kotlin file operations โ€บ kotlin โ€“ read csv file line by line
Kotlin โ€“ Read CSV File Line by Line - Kotlin Android
To read a CSV file line by line, read the file into a File object, and then use File.forEachLine() function to process the file line by line. In this tutorial, we will go through the process of reading a CSV file line by line using Kotlin, with an example.