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 - Reads a file line by line using the specified charset (default is UTF-8) and calls action for each line: fun readFileLineByLineUsingForEachLine(fileName: String) = File(fileName).forEachLine { println(it) }
๐ŸŒ
ZetCode
zetcode.com โ€บ kotlin โ€บ readfile
Kotlin read file - reading file in Kotlin
January 29, 2024 - With forEachIndexed we add a line number to each line. File.readText gets the entire content of this file as a String.
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ kotlin โ€บ kotlin-read-file-into-string
How to read a file into a string in Kotlin?
May 9, 2023 - ... import java.io.File fun main() { val file = File("info.txt") val text = file.readText() println(text) } ... In this Kotlin Tutorial, we learned how to read contents a file into a string using java.io.File.readText() function.
๐ŸŒ
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 - File.readText() : To read contents of file to a single String ยท File.useLines() : To process a large text file lazily ... Prepare file object with the location of the file passed as argument to File class constructor.
๐ŸŒ
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....
๐ŸŒ
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 ...
๐ŸŒ
Programiz
programiz.com โ€บ kotlin-programming โ€บ examples โ€บ string-from-file
Kotlin Program to Create String from Contents of a File
import java.nio.charset.Charset import java.nio.file.Files import java.nio.file.Paths fun main(args: Array<String>) { val path = System.getProperty("user.dir") + "\\src\\test.txt" val encoding = Charset.defaultCharset(); val lines = Files.readAllLines(Paths.get(path), encoding) println(lines) } ... In the above program, we use System's user.dir property to get the current directory stored in the variable path. Check Kotlin Program to get the current directory for more information.
Find elsewhere
๐ŸŒ
Kotlin
kotlinlang.org โ€บ api โ€บ core โ€บ kotlin-stdlib โ€บ kotlin.io โ€บ read-text.html
readText | Core API โ€“ Kotlin Programming Language
This method is not recommended on huge files. It has an internal limitation of 2 GB file size. ... Reads this reader completely as a String. Note: It is the caller's responsibility to close this reader.
๐ŸŒ
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.
๐ŸŒ
Kotlin
kotlinandroid.org โ€บ kotlin android home โ€บ kotlin tutorials โ€บ kotlin file operations โ€บ kotlin โ€“ read file
Kotlin โ€“ Read File
To reading a file in Kotlin, you can use 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.
๐ŸŒ
FrontBackend
frontbackend.com โ€บ kotlin โ€บ how-to-read-a-file-in-kotlin
How to read a file in Kotlin | FrontBackend
March 17, 2021 - import java.io.File import java.io.InputStream fun main(args: Array<String>) { val inputStream: InputStream = File("frontbackend.txt").inputStream() val out = mutableListOf<String>() inputStream.bufferedReader().useLines { lines -> lines.forEach { out.add(it) } } out.forEach { println(it) } } ... In this short article, we presented ways to read a file in Kotlin.
๐ŸŒ
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 - To read file content as a List<String> in Kotlin, use the File.readLines() extension function from java.io.File.
๐ŸŒ
Techie Delight
techiedelight.com โ€บ read-a-text-file-into-string-in-kotlin
Read a text file into a string in Kotlin
March 12, 2022 - Techie Delight is a leading platform for technical interview preparation. Prepare for your coding interview with Techie Delight. Improve your algorithmic skills and crack interviews with top tech companies.
๐ŸŒ
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 provides an expressive and concise syntax for handling file operations. Its seamless interoperability with Java libraries means you can use familiar java.io classes alongside Kotlin-specific utilities to read file content efficiently.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ read-from-files-using-inputreader-in-kotlin
Read From Files using InputReader in Kotlin - GeeksforGeeks
March 13, 2022 - If we need to read a file located in a different folder, it looks similar to the following: ... This piece of code simply takes all the text in the file and prints it on the console. Another way of reading file contents is by directly creating a reader of the file as we do in this code: ... import java.io.File fun main (args: Array<String>) { val inputString = File("gfg.txt").reader().use{it.readText()} println (inputString) }
๐ŸŒ
GitHub
gist.github.com โ€บ laaptu โ€บ 6459577
Android read file as string from asset ยท GitHub
import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.widget.Toast import kotlinx.android.synthetic.main.activity_main.* import java.io.BufferedReader import java.io.IOException import java.io.InputStreamReader ... override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val dictfile = getString(R.string.TestFile) try { val fIn = (assets.open(dictfile)) val file = InputStreamReader (fIn) val br = BufferedReader (file) var line = br.readLine () val all = StringBuilder () while (line!= null) { all.append (line + "\n") line = br.readLine () } br.close () file.close () Results.text = all } catch (e: IOException) { Toast.makeText (this, " Could not read the file called < $dictfile > ", Toast.LENGTH_LONG) .show () } }
๐ŸŒ
Hyperskill
hyperskill.org โ€บ learn โ€บ step โ€บ 6351
Reading files ยท Hyperskill
May 22, 2023 - First, there is readText() which helps you read the whole file in just one String variable. Say we create a text file reading.txt with the following text: Kotlin or Java, Java or C++. We put it in the src folder, and now we can read it with ...
๐ŸŒ
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. For reading lines of a large file or a file of unknown size, use Path.forEachLine or Path.useLines.
๐ŸŒ
Chercher
chercher.tech โ€บ null โ€บ null โ€บ read and write files in kotlin
Read and write files in Kotlin
August 29, 2018 - Chercher tech provides the tutorials for the future technologies, We are writing an article one per day Actually the author is not a developer, he is a tester first author thought to write a tutorial only for selenium then he thought for protractor python and then UI path now writing kotlin file operations ยท bufferedReader() to read contents of a file into BufferedReader