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 OverflowBaeldung
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:
Top answer 1 of 8
86
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) }
}
2 of 8
40
I think the simplest way to code is using kotlin.text and java.io.File
import java.io.File
fun main(args: Array<String>) {
val text = File("sample.txt").readText()
println(text)
}
Videos
18:16
Working With Files In Kotlin - IO Essentials - YouTube
07:00
Efficient IO With Buffered Reading & Writing In Kotlin - IO ...
08:42
Kotlin Day 13 | File IO in Kotlin Explained | Programming with ...
03:08
Read Files from Assets in Android | Kotlin Tutorial - YouTube
06:08
Read Files from Resources in Android | Kotlin Tutorial - YouTube
06:07
Read and display text file in android 11. - YouTube
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
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.
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....