You can use the writeBytes function:

fun File.writeBytes(array: ByteArray)
Answer from André Jesus on Stack Overflow
🌐
Medium
medium.com › @vahidnety › save-bytearray-data-into-a-file-in-android-kotlin-a297c6800184
Save bytearray data into a file in android Kotlin | by Vahid D | Medium
May 1, 2023 - To save a ByteArray to a file in Kotlin on Android, you can use the FileOutputStream class along with the write() method. Here's an example…
🌐
Baeldung
baeldung.com › home › kotlin › kotlin io › writing to a file in kotlin
Writing to a File in Kotlin | Baeldung on Kotlin
April 28, 2026 - The method writeBytes takes a ByteArray as an argument and directly writes it into the specified file. This is useful when we have the contents as an array of bytes rather than plain text.
Discussions

About write a UByteArray to the file - Libraries - Kotlin Discussions
Is there any way to write a UbyteArray to the file directly? More on discuss.kotlinlang.org
🌐 discuss.kotlinlang.org
0
January 8, 2019
android - Converting a byte array to a pdf file then saving it - Stack Overflow
I'm hitting an API Endpoint with a Kotlin Android app. In this API call I'm returning a byte Array. What I would like to see is a way to convert the byte array to a pdf file and save it in the phones download folder. private suspend fun getIDCard(sessionID: String?, carriermemid: String?): ByteArray... More on stackoverflow.com
🌐 stackoverflow.com
android - kotlin reading from file into byte array - Stack Overflow
How do I read bytes into a Byte Array? In java I used to initialize byte array as byte[] b = new byte[100] and then pass that to the corresponding method. However in Kotlin, I am unable to initial... More on stackoverflow.com
🌐 stackoverflow.com
android - Create file from Byte Array sent inside JSON Object kotlin - Stack Overflow
Everything is done according to Kotlin Coroutines, so the async aspect is taken care of and not part of the problem. ... But FileOutputStream requires Int or ByteArray as arguments. More on stackoverflow.com
🌐 stackoverflow.com
September 21, 2017
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.io › java.io.-file › write-bytes.html
writeBytes - Kotlin Programming Language
August 18, 2022 - kotlin-stdlib / kotlin.io / java.io.File / writeBytes · JVM · 1.0 · fun File.writeBytes(array: ByteArray) (source) Sets the content of this file as an array of bytes. If this file already exists, it becomes overwritten. array - byte array ...
🌐
Android Developers
developer.android.com › api reference › bytearrayoutputstream
ByteArrayOutputStream | 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 › write-bytes.html
writeBytes | Core API – Kotlin Programming Language
kotlin-stdlib/kotlin.io/writeBytes · JVM · fun File.writeBytes(array: ByteArray)(source) Sets the content of this file as an array of bytes. If this file already exists, it becomes overwritten.
🌐
Educative
educative.io › answers › how-to-write-to-a-file-in-kotlin
How to write to a file in Kotlin
text: We use this parameter text/string to write to a file. ... The writeBytes() method writes a ByteArray to the specified file.
Find elsewhere
🌐
Programiz
programiz.com › kotlin-programming › examples › convert-file-byte-array
Kotlin Program to Convert File to byte array and Vice-Versa
We also have a finalPath where the bytes are to be written. Then, we simply use the Files' write() method to write the encoded byte array to a File in the given finalPath.
🌐
Kotlinlang
slack-chats.kotlinlang.org › t › 22798000 › is-there-any-library-in-kmp-can-do-convert-from-bytearray-in
Is there any library in kmp can do convert from byteArray in kotlinlang #multiplatform
See how file. ReadBytes just returns the byte array so you don't need to save the file ... yeah but the headers required contentType and contentDisposition. so in this case how do i get this information from byteArray?
🌐
Android Developers
developer.android.com › api reference › bytearrayinputstream
ByteArrayInputStream | 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 · 中文 – 简体
🌐
Stack Overflow
stackoverflow.com › questions › 46346208 › create-file-from-byte-array-sent-inside-json-object-kotlin
android - Create file from Byte Array sent inside JSON Object kotlin - Stack Overflow
September 21, 2017 - Everything is done according to Kotlin Coroutines, so the async aspect is taken care of and not part of the problem. ... But FileOutputStream requires Int or ByteArray as arguments.
🌐
GitHub
github.com › JetBrains › kotlin › blob › master › libraries › stdlib › jvm › src › kotlin › io › FileReadWrite.kt
kotlin/libraries/stdlib/jvm/src/kotlin/io/FileReadWrite.kt at master · JetBrains/kotlin
if (length > Int.MAX_VALUE) throw OutOfMemoryError("File $this is too big ($length bytes) to fit in memory.") ... public fun File.appendBytes(array: ByteArray): Unit = FileOutputStream(this, true).use { it.write(array) }
Author   JetBrains
🌐
Medium
alessandromautone.medium.com › kotlin-copy-big-files-fb86f35abfca
Kotlin — Copy (big) files - Alessandro Mautone - Medium
February 10, 2020 - We can read the bytes ourself using a buffer: we can read chunks of bytes, store them in a buffer and then append those chunks one by one to the destination file. ... A sample of how this is implemented can be found at https://github.com/Alexs784/Kotlin-Copy-file-sample
🌐
TutorialKart
tutorialkart.com › kotlin › kotlin-create-file
Kotlin - Create File - Examples
March 22, 2023 - You may provide the string you would like to write into this file. File.writeBytes() creates a new file if it does not exist already and writes the raw bytes of the ByteArray provided to the file created.
🌐
Android Developers
developer.android.com › api reference › randomaccessfile
RandomAccessFile | 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 · 中文 – 简体
🌐
Reddit
reddit.com › r/kotlin › how to efficiently write bytes to file with buffer?
r/Kotlin on Reddit: How to efficiently write bytes to file with buffer?
July 18, 2021 -

I am making my first evolutionary sim while also learning Kotlin. I want to save the state of epochs (what happens in each simulation) and am making a binary encoding to help with efficiency. I want to write bytes to a file in a buffered fashion (write every 10kb or something).

I am trying to attack this by writing a ByteArray and tracking the number of bytes "added", and then writing and resetting when it hits the limit. This would require a conditional statement for every byte I write to the buffer though, I was wondering if this is naive?

Thanks in advance for any help!

🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.io › java.io.-file › append-bytes.html
appendBytes - Kotlin Programming Language
kotlin-stdlib / kotlin.io / java.io.File / appendBytes · JVM · 1.0 · fun File.appendBytes(array: ByteArray) (source) Appends an array of bytes to the content of this file.