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 Overflow08:13
Create Boilerplate Files With ONE Click With Multi-File Templates ...
18:16
Working With Files In Kotlin - IO Essentials - YouTube
01:19
How to Create a Kotlin File in IntelliJ - YouTube
05:54
Kotlin How to Write to Text File - YouTube
17:07
How to Read / Write To File - Android Studio/Kotlin - YouTube
THIS is HOW you can SELECT FILES using KOTLIN - Android - FILE ...
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
kotlinandroid.org › kotlin android home › kotlin tutorials › kotlin file operations › kotlin – create file
Kotlin – Create File
To create a file in Kotlin, you can utilize the File class from the java.io package.
Kotlin
kotlinandroid.org › kotlin android home › kotlin tutorials › kotlin file operations › kotlin – create text file
Kotlin – Create text file
To create a text file in Kotlin, you can use File and FileWriter classes of java.io package.
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.
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...