openFileOutput() takes a filename, not a path. openFileOutput() returns an OutputStream on a file that is located in the directory identified by getFilesDir() (a.k.a., filesDir in Kotlin).

Try:

File(applicationContext.filesDir, "test.txt").printWriter().use { out ->
    out.println("${it.key}, ${it.value}")
}

or:

applicationContext.openFileOutput("test.txt", Context.MODE_PRIVATE).use 
{
    it.write("test".toByteArray())
}
Answer from CommonsWare on Stack Overflow
🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin.io.path › create-file.html
createFile | Core API – Kotlin Programming Language
kotlin-stdlib/kotlin.io.path/createFile · JVM · @IgnorableReturnValueinline fun Path.createFile(vararg attributes: FileAttribute<*>): Path(source) Creates a new and empty file specified by this path, failing if the file already exists. 1.5 · this path. attributes ·
🌐
TutorialKart
tutorialkart.com › kotlin › kotlin-create-file
Kotlin - Create File - Examples
March 22, 2023 - Most of the other methods, we go through in this tutorial, would overwrite the file if it exists which may result in the loss of existing data which is not desired in some cases. In the following example, we try to create a new file with name data.txt. For the first time, the file is created and true is returned.
🌐
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 - We’ll use several of these to demonstrate different ways in which we can achieve this using Kotlin: writeText – lets us write directly from a String · writeBytes – enables us to write directly from a ByteArray ... Let’s discuss them in more detail. Writing directly into a File from a given source is the simplest strategy that we can expect using Kotlin extension methods.
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.io.path › java.nio.file.-path › create-file.html
createFile - Kotlin Programming Language
September 6, 2022 - Try the revamped Kotlin docs design! ... Creates a new and empty file specified by this path, failing if the file already exists.
🌐
BezKoder
bezkoder.com › home › ways to write to file in kotlin
Ways to write to File in Kotlin - BezKoder
January 24, 2024 - Files.write(path, bytes, StandardOpenOption.CREATE_NEW); Files.write(path, bytes, StandardOpenOption.APPEND); You can find more options here: StandardOpenOption ... package com.bezkoder.kotlin.writefile import java.io.File import java.nio.file.Files ...
Find elsewhere
🌐
Android Developers
developer.android.com › api reference › files
Files | 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 Discussions
discuss.kotlinlang.org › support
Why cant I create and write a file? - Support - Kotlin Discussions
August 7, 2024 - Hello, I want to generate files with source code in them. For that I do the following (an excerpt): fun generate_AST_nodes(grammar:List , class_name:String) { var file_exists:Boolean = false if (File(directory_path + "/" + class_name + ".kt").exists()) file_exists = true var class_file:File = File(directory_path + "/" + class_name + ".kt") class_file.createNewFile() if (!file_exists) class_file.writeText("abstract " + c...
🌐
ZetCode
zetcode.com › kotlin › writefile
Kotlin write file - learn how to write a file in Kotlin
January 29, 2024 - The bufferedWriter returns a BufferedWriter for writing the content to the file. The use method executes the given block function on the file and then closes it. The writeText is a Kotlin File extension function which writes text encoded using UTF-8 or other charset to the file.
🌐
TutorialsPoint
tutorialspoint.com › how-to-create-a-text-file-and-insert-data-to-that-file-on-android-using-kotlin
How to create a text file and insert data to that file on Android using Kotlin?
import android.content.Context import android.os.Bundle import android.view.View import android.widget.EditText import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import java.io.FileOutputStream import java.io.OutputStreamWriter class MainActivity : AppCompatActivity() { lateinit var editText: EditText override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" editText = findViewById(R.id.editText) } fun saveTextFile(view: View) { try { val fileOutputStream: FileOutputStream = openF
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.io › create-temp-file.html
createTempFile - Kotlin Programming Language
July 9, 2021 - Use kotlin.io.path.createTempFile instead or resort to java.io.File.createTempFile. Creates a new empty file in the specified directory, using the given prefix and suffix to generate its name.
🌐
Chercher
chercher.tech › null › null › read and write files in kotlin
Read and write files in Kotlin
August 29, 2018 - The file created is empty and has zero bytes written to it. createNewFile() is the way in which the existing file will not be overridden. Remaining ways will overwrite the file if it exists.
🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin.io.path › create-temp-file.html
createTempFile | Core API – Kotlin Programming Language
fun createTempFile(directory: Path?, prefix: String? = null, suffix: String? = null, vararg attributes: FileAttribute<*>): Path(source)
🌐
Sling Academy
slingacademy.com › article › how-to-create-files-in-kotlin
How to Create Files in Kotlin - Sling Academy
Import the necessary package: Kotlin is interoperable with Java, so we can use java.io.File for file operations. ... Create a new file: Use the File constructor to define the path and filename.
🌐
CodeSignal
codesignal.com › learn › courses › fundamentals-of-text-data-manipulation-in-kotlin › lessons › writing-to-files-in-kotlin
Writing to Files in Kotlin | CodeSignal Learn
When executed, this sequence of operations will create a file named output.txt (if it doesn't exist), write the specified lines of text into it, and overwrite the content if the file already exists. The content of the file after executing Files.write will look like this: ... Sometimes, you may want to add data to an existing file without overwriting its current contents. This can be easily achieved in Kotlin using the Files.write method with the StandardOpenOption.APPEND flag.
🌐
Baeldung
baeldung.com › home › kotlin › kotlin io › how to create a temporary file in kotlin
How to Create a Temporary File in Kotlin | Baeldung on Kotlin
May 31, 2024 - This approach enhances the readability and conciseness of file management tasks: fun createTempFileWithKotlinExtensions(): Path { val tempFile = createTempFile(prefix = "kotlinTemp", suffix = ".tmp") println("Temporary file created with Kotlin extensions at: ${tempFile.toAbsolutePath()}") tempFile.writeText("Kotlin Path Data") return tempFile }
🌐
Stack Overflow
stackoverflow.com › questions › 75426371 › how-to-create-a-file-in-the-resources-folder-in-kotlin-gradle
How to create a file in the resources folder in Kotlin, Gradle - Stack Overflow
In a completely fresh project, I want to create a single file myFile.json inside the src/main/resources/ folder at run time. For reading a file, I need to do some config in the build.gradle.kts fil...