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 the file created above as the input for the initial methods and the relative 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:
People also ask

How to read from a JSON file in Kotlin?
To read a JSON file in Kotlin, use libraries like Kotlinx Serialization, Gson, or Moshi. First, read the file content as a string, then parse it into a Kotlin object using the chosen library's deserialization features.
๐ŸŒ
dhiwise.com
dhiwise.com โ€บ post โ€บ simplifying-kotlin-read-from-file-a-practical-approach
Kotlin Read From File: Step-by-Step Instructions Simplified
How do I open a Kotlin file?
To open a Kotlin file programmatically, use the File class from `java.io`. Specify the file's path and utilize methods like `readText()` or `bufferedReader()` to access its content. Ensure the file path is correct and accessible.
๐ŸŒ
dhiwise.com
dhiwise.com โ€บ post โ€บ simplifying-kotlin-read-from-file-a-practical-approach
Kotlin Read From File: Step-by-Step Instructions Simplified
How to use readln in Kotlin?
The `readln()` function is used to read input from the console, not from files. It captures a single line of input as a string. For file reading, use methods like`File.readText()` or BufferedReader.
๐ŸŒ
dhiwise.com
dhiwise.com โ€บ post โ€บ simplifying-kotlin-read-from-file-a-practical-approach
Kotlin Read From File: Step-by-Step Instructions Simplified
๐ŸŒ
ZetCode
zetcode.com โ€บ kotlin โ€บ readfile
Kotlin read file - reading file in Kotlin
January 29, 2024 - Kotlin read file tutorial shows how to read a file in Kotlin. We show several ways of reading a file in Kotlin.
๐ŸŒ
DhiWise
dhiwise.com โ€บ post โ€บ simplifying-kotlin-read-from-file-a-practical-approach
Kotlin Read From File: Step-by-Step Instructions Simplified
January 21, 2025 - Kotlin also helps you avoid common pitfalls, like resource leaks, by making it easy to automatically close streams and readers. Before diving into the code, ensure your Kotlin project is configured correctly. Youโ€™ll often need to work with classes like BufferedReader or InputStream from Java. To get started, include import java.io.* in your file.
๐ŸŒ
Kotlin
kotlinlang.org โ€บ api โ€บ core โ€บ kotlin-stdlib โ€บ kotlin.io โ€บ read-text.html
readText | Core API โ€“ Kotlin Programming Language
kotlin-stdlib/kotlin.io/readText ยท JVM ยท fun File.readText(charset: Charset = Charsets.UTF_8): String(source) Gets the entire content of this file as a String using UTF-8 or specified charset.
๐ŸŒ
Kotlin
kotlinandroid.org โ€บ kotlin android home โ€บ kotlin tutorials โ€บ kotlin file operations โ€บ kotlin โ€“ read file
Kotlin โ€“ Read File
Reading a file in Kotlin can be efficiently done using the readText() method of the File class from the java.io package. This method reads the entire content of a file into a single String, making it convenient for smaller files.
๐ŸŒ
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 - Happy learning Kotlin. Process finished with exit code 0 ยท Content of file is printed to the console. forEachLine() is useful when you do not need to store all lines. For example, you can count matching lines, parse each line, or print each line as it is read.
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @YodgorbekKomilo โ€บ day-18-file-handling-in-kotlin-reading-writing-files-made-simple-da774d449459
๐Ÿ“ Day 18 โ€” File Handling in Kotlin: Reading & Writing Files Made Simple! | by Yodgorbek Komilov | Medium
May 5, 2025 - ๐Ÿ‘‰ Remember to always close streams (or use use), handle exceptions properly, and check if a file exists before reading it! Thatโ€™s it for Day 18 โ€” stay tuned for more Kotlin adventures!
๐ŸŒ
BezKoder
bezkoder.com โ€บ home โ€บ how to read file in kotlin
How to read File in Kotlin - BezKoder
February 16, 2020 - Way to read File in Kotlin line-by-line/all lines using InputStream or BufferedReader or File directly.
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ kotlin โ€บ kotlin โ€“ reading large files efficiently
Kotlin - Reading Large Files Efficiently - Mkyong.com
March 3, 2025 - Reading large files efficiently in Kotlin is essential for performance optimization. We can use BufferedReader for line-by-line processing, useLines for lazy sequence processing, and InputStream for binary files.
๐ŸŒ
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.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-read-a-text-file-from-resources-in-kotlin
How to read a text file from resources in Kotlin?
import java.io.File import java.io.InputStream fun main(args: Array<String>) { val inputStream: InputStream = File("Text.txt").inputStream() val inputString = inputStream.bufferedReader().use { it.readText() } println(inputString) } ... [Running] cd "<<Your workspace>>" && kotlinc readFile.kt -include-runtime -d readFile.jar && java -jar readFile.jar Welcome to the best tutorial website - www.tutorialsPoint.com This is the Text file that we want to read via Kotlin
๐ŸŒ
Kotlin
kotlinlang.org โ€บ api โ€บ latest โ€บ jvm โ€บ stdlib โ€บ kotlin.io โ€บ java.io.-file โ€บ read-text.html
readText - Kotlin Programming Language
December 30, 2022 - 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 ...
๐ŸŒ
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 - The tutorial shows that File.readLines() returns List<String>. The examples include the required import java.io.File or kotlin.io.path imports.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ kotlin โ€บ read-from-files-using-inputreader-in-kotlin
Read From Files using InputReader in Kotlin - GeeksforGeeks
July 23, 2025 - The decision to use any method is based on whether we want the stream to be closed on its own after execution, or we want to handle closing the resources, and whether or not we want to read from a stream or directly from the file. BufferedReader reads a couple of characters at a time from the input stream and stores them in the buffer.
๐ŸŒ
Kotlin Discussions
discuss.kotlinlang.org โ€บ language design
What's the intended way to read a resource file? - Language Design - Kotlin Discussions
December 15, 2021 - I have a text file in the resources directory and I want to read it. Currently, Iโ€™ve managed to do it in the following ways: // specifying the full path val lines = File("src/main/resources/other_subdirectories/file.txt").readLines() // only works inside a class val lines = this::class.java.getResourceAsStream("file.txt")?.bufferedReader()?.readLines() // works otherwise val lines = object {}.javaClass.getResourceAsStream("file.txt")?.bufferedReader()?.readLines() Admittedly, none of the thr...
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ fundamentals-of-text-data-manipulation-in-kotlin โ€บ lessons โ€บ reading-files-line-by-line-in-kotlin-and-summing-integer-values
Reading Files Line-by-Line in Kotlin and Summing Integer ...
Imagine needing to process a set of numbers stored in a file โ€” reading each integer and calculating their sum. This is a practical task you can easily accomplish using Files.readAllLines and basic string manipulation in Kotlin.
๐ŸŒ
Kotlin
kotlinlang.org โ€บ docs โ€บ data-analysis-work-with-data-sources.html
Retrieve data from files | Kotlin Documentation
3 weeks ago - ... Run the code cell with the ... use DataFrame as a Gradle or Maven dependency. To retrieve data from a file, use the DataFrame.read() function:...
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ kotlin โ€บ kotlin โ€“ read, write, modify, delete, and list files
Kotlin - Read, Write, Modify, Delete, and List Files - Mkyong.com
March 3, 2025 - In this article, we will cover how to read, write, modify, delete, and list files in Kotlin and compare them with Java equivalents.
๐ŸŒ
Kotlin
kotlinandroid.org โ€บ kotlin android home โ€บ kotlin tutorials โ€บ kotlin file operations โ€บ kotlin โ€“ read file to string
Kotlin โ€“ Read File to String
import java.io.File fun main() ... this Kotlin File Operations tutorial, we have seen how to read a text file to a string using java.io.File.readText() method, with a well detailed step by step process, and an example program....