You can simplify your function by using the copyTo function:

fun File.copyInputStreamToFile(inputStream: InputStream) {
    this.outputStream().use { fileOut ->
        inputStream.copyTo(fileOut)
    }
}
Answer from yole on Stack Overflow
🌐
Android Developers
developer.android.com › api reference › fileinputstream
FileInputStream | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin.io › input-stream.html
inputStream | Core API – Kotlin Programming Language
kotlin-stdlib/kotlin.io/inputStream · JVM · inline fun File.inputStream(): FileInputStream(source) Constructs a new FileInputStream of this file and returns it as a result. 1.0 · inline fun ByteArray.inputStream(): ByteArrayInputStream(source) ...
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.io › java.io.-file › input-stream.html
inputStream - Kotlin Programming Language
Try the revamped Kotlin docs design! ... Constructs a new FileInputStream of this file and returns it as a result.
🌐
Baeldung
baeldung.com › home › kotlin › kotlin io › writing inputstream to file in kotlin
Writing InputStream to File in Kotlin | Baeldung on Kotlin
April 28, 2026 - This function copies everything from the receiving InputStream to the given OutputStream: val content = "Hello World".repeat(1000) val file: File = createTempFile() val inputStream = ByteArrayInputStream(content.toByteArray()) inputStream.use ...
🌐
Compile N Run
compilenrun.com › kotlin tutorial › kotlin io › kotlin input output streams
Kotlin Input Output Streams | Compile N Run
Here's a basic example of reading ... fun main() { val file = File("sample.txt") // Using FileInputStream (byte stream) FileInputStream(file).use { fis -> val bytes = ByteArray(file.length().toInt()) fis.read(bytes) println("File content (as bytes converted to string): ${String(bytes)}") } // More idiomatic ...
🌐
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 - Kotlin Read File - To read contents of file, use extension methods : Kotlin bufferedReader() - read file to BufferedReader; inputStream() - read file to InputStream; Kotlin forEachLine() - read file line by line; readBytes() - read file to ...
🌐
O'Reilly
oreilly.com › library › view › kotlin-programming-by › 9781788474542 › 9746b052-8766-427d-b298-26eb5d9bf64f.xhtml
Reading files from internal storage - Kotlin Programming By Example [Book]
March 28, 2018 - private fun readFile(fileName: String) { val stream: FileInputStream = openFileInput(fileName) val data = ByteArray(1024) stream.read(data) stream.close()}
Author   Iyanu Adelekan
Published   2018
Pages   500
Find elsewhere
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › read-from-files-using-inputreader-in-kotlin
Read From Files using InputReader in Kotlin - GeeksforGeeks
March 13, 2022 - Basically, kotlin.io provides a nice, clean API for reading from and writing to files. In this article, we are going to discuss how to read from files using inputReader in Kotlin. One way of doing this is by using inputreader.
🌐
Medium
medium.com › @sujitpanda › file-read-write-with-kotlin-io-31eff564dfe3
File Read/Write with Kotlin IO. In this post i will be explaining how… | by Sujit Panda | Medium
February 13, 2024 - File Read/Write with Kotlin IO In this post, I will be explaining how to read/write files in Kotlin. I was working on an Android project that was written in Java. I was trying to port one Java file …
🌐
Medium
medium.com › @devrath.dev595 › handling-input-stream-using-kotlin-dbc404f03967
Handling input stream using kotlin | by Devrath | Medium
February 20, 2022 - Handling input stream using kotlin In earlier days prior to kotlin Handling the input stream used to be a challenge. Whenever we think of reading data from a file stored in a project, That can …
🌐
ZetCode
zetcode.com › kotlin › readfile
Kotlin read file - reading file in Kotlin
January 29, 2024 - The example reads a text file into a byte array. It prints the file as numbers to the console. ... In this article we have shown how to read file in Kotlin.
🌐
Android Developers
developer.android.com › api reference › inputstream
InputStream | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
YouTube
youtube.com › watch
InputStreams & OutputStreams In Kotlin - IO Essentials - YouTube
In this video you'll learn about input and output streams in Kotlin and how we can use these to read data from and write data to files. ⭐ Courses with real-l...
Published   January 22, 2025
🌐
Programiz
programiz.com › kotlin-programming › examples › convert-inputstream-string
Kotlin Program to Convert InputStream to String
In this program, you'll learn to convert input stream to a string using InputStreamReader in Kotlin.
🌐
Medium
kabi20.medium.com › i-o-streams-in-kotlin-5a4d00107167
I/O Streams in Kotlin | Medium
May 10, 2026 - In Kotlin instead of manually opening ... then it closes the stream. fun main() { val file = File("hello.txt") val bytes = FileInputStream(file).use { it.readBytes() } println(bytes.decodeToString()) }...