val fileContent = MySpec::class.java.getResource("/html/file.html").readText()
Answer from JB Nizet on Stack OverflowStack 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
Here is code that writes back to where the source resources folder is: fun main(args: Array<String>) { // Let's assume you want your project to be portable, so you don't // want to use absolute file paths.
ZetCode
zetcode.com › kotlin › writefile
Kotlin write file - learn how to write a file in Kotlin
January 29, 2024 - We write to src/resources/myfile.txt file. ... The printWriter returns a PrintWriter for writing the content to the file.
What's the intended way to read a resource file? - Language Design - Kotlin Discussions
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… More on discuss.kotlinlang.org
How to write a file to app specific directory? - Support - Kotlin Discussions
I made a post a while back but the question went unanswered. The problem I am having still persists and I am not sure how to solve it. The problem is that I am trying to write a file to my app specific directory but when I write the file it does not save to the app directory. More on discuss.kotlinlang.org
KMM: Write file to directory
Check out okio https://square.github.io/okio/multiplatform/ More on reddit.com
kotlin - How do i create and fill a file in the directory the User choose with ACTION_OPEN_DOCUMENT_TREE on Android 9-11 - Stack Overflow
I try to make an Export Function for my App, that can export(and later Import) data to a txt-file in the shared storage. I use the ACTION_OPEN_DOCUMENT_TREE to choose a Folder and get "resultd... More on stackoverflow.com
Top answer 1 of 15
224
val fileContent = MySpec::class.java.getResource("/html/file.html").readText()
2 of 15
93
No idea why this is so hard, but the simplest way I've found (without having to refer to a particular class) is:
fun getResourceAsText(path: String): String? =
object {}.javaClass.getResource(path)?.readText()
It returns null if no resource with this name is found (as documented).
And then passing in an absolute URL, e.g.
val html = getResourceAsText("/www/index.html")!!
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 - Kotlin also offers extension methods that provide us with a Java Writer instance. If we’d like to use a Java PrintWriter, Kotlin provides a printWriter function exactly for this purpose. With it, we can print formatted representations of objects to an OutputStream: ... This method returns a new PrintWriter instance. Next, we can take advantage of the method use to handle it: File(fileName).printWriter().use { out -> out.println(fileContent) } With use, we can execute a function on the resource which gets closed after termination.
Kotlinlang
slack-chats.kotlinlang.org › t › 8444306 › hello-is-there-any-way-to-write-files-in-resources-in-shared
Hello Is there any way to write files in resources in shared kotlinlang #multiplatform
January 16, 2023 - Ordinarily I'd expect to be able to add the common resources path to the platform src sets. But if you do this, the IDE will remove the src paths with the error: "Duplicate content roots detected", as the IDE doesn't support having the same source directory belong to multiple modules or source sets. Symlinks work as a workaround as the IDE sees the files as unique paths.
Android Developers
developer.android.com › android studio › add app resources
Add app resources | Android Studio | Android Developers
May 15, 2026 - This page describes how to add basic resource files, how to change the location of your resources, and how resource merging works. In Jetpack Compose, you define your UI in Kotlin rather than XML layouts. However, you still use the res/ directory to store app-level static assets like icons, fonts, and translations.
BezKoder
bezkoder.com › home › how to read file in kotlin
How to read File in Kotlin - BezKoder
February 16, 2020 - Related Posts: – Kotlin Android – Read JSON file from assets using Gson – Ways to write to File in Kotlin · First, we need a file to read. So create a file named bezkoder.txt with following content: bezkoder.com -> Programming Tutorial -> Becoming zKoder -> Master Programming · Because we don’t indicate path for bezkoder.txt file, so you should put it in the root folder of the Project (the folder src is located in). If you wanna read file in resources folder \app\src\main\res\, just change the path: File("bezkoder.txt") -> File("src/main/res/bezkoder.txt")
Palplanner
palplanner.com › schools › kotlin-file-and-resource-management-a-comprehensive-guide
Kotlin File and Resource Management: A Comprehensive Guide – Schools
Writing binary data is similar to reading binary data. You create a byte array and use the writeBytes() function to save it to a file. Kotlin provides a powerful way to manage resources in Android applications, which are often packaged within the APK file.
Android Developers
developer.android.com › api reference › resources
Resources | 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 · 中文 – 简体
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
How to write a file to app specific directory? - Support - Kotlin Discussions
March 21, 2022 - I made a post a while back but the question went unanswered. The problem I am having still persists and I am not sure how to solve it. The problem is that I am trying to write a file to my app specific directory but when I write the file it does not save to the app directory.
Reddit
reddit.com › r/kotlin › kmm: write file to directory
r/Kotlin on Reddit: KMM: Write file to directory
January 17, 2024 -
Hello, I'm basically a KMM noob, have written many iOS projects but just a few Android ones. I'm creating a KMM library that would be used by an Android app and an iOS one.
What I want to do is to write/read some content into each platform default directory, typically the Document directory of iOS, and the equivalent on Android.
What is the go-to way to do that with KMM?
Thanks a lot for the help!
Top answer 1 of 5
5
Check out okio https://square.github.io/okio/multiplatform/
2 of 5
4
Hi 👋 the author of KStore , a simple library that lets you save objects to disk serialised via kotlinx serialisation, using Okio and provide a nice Kotlin Coroutines API. You can use my library, or just replicate what I'm doing for your usecase I have documentation here that covers how you'd get an Okio path on iOS Here's an in-depth guide on where files can be stored on iOS https://tanaschita.com/20221010-quick-guide-on-the-ios-file-system/
LinkedIn
linkedin.com › all topics › technology › software development › programming languages
Kotlin Essential Training: Functions, Collections, and I/O Online Class | LinkedIn Learning, formerly Lynda.com
Get a comprehensive overview of what you need to know as a developer to start writing command-line programs in the Kotlin programming language.
Published April 29, 2022
Kotlinlang
slack-chats.kotlinlang.org › t › 16196924 › i-have-resources-placed-in-resources-directory-of-the-common
I have resources placed in resources directory of the common kotlinlang #multiplatform
December 27, 2023 - tasks.register("PreCommitMessage", Copy::class) { from(file("${rootProject.rootDir}/commit-msg")) into(File(rootProject.rootDir, ".git/hooks")) } What are you using for accessing that shared resources folder on iOS? I'm using: NSBundle.mainBundle.resourcePath + "/compose-resources/" + asset // asset is name of the resource ... @Michal Havryluk I went to the emulator folder in Finder and opened the bundle itself and didn’t find anything there.
Stack Overflow
stackoverflow.com › questions › 68058372 › how-do-i-create-and-fill-a-file-in-the-directory-the-user-choose-with-action-ope
kotlin - How do i create and fill a file in the directory the User choose with ACTION_OPEN_DOCUMENT_TREE on Android 9-11 - Stack Overflow
fun createFile() { if(permissedFolder.data != null) { val folderUri:Uri = permissedFolder.data!! val enabledDirectory:DocumentFile= DocumentFile.fromTreeUri(this, folderUri)!! val spellsExportDF =enabledDirectory.createFile(".txt","Spells.txt") val spellsExportURI = spellsExportDF?.uri if(spellsExportURI != null) { val fileOutputStream = getContentResolver().openOutputStream(spellsExportURI) //FileOutputStream(spellsExportURI, true) fileOutputStream.use { fileout -> fileout?.writer(Charsets.UTF_8)?.use { it.write(test) it.flush() it.close() } } val readfile = spellsExportDF.canRead() val writefile =spellsExportDF.canWrite() Toast.makeText(this, "can Read: $readfile", Toast.LENGTH_SHORT).show() Toast.makeText(this, "can Write: $writefile", Toast.LENGTH_SHORT).show() }
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
Baeldung
baeldung.com › home › kotlin › kotlin io › reading from a file in kotlin
Reading from a File in Kotlin | Baeldung on Kotlin
April 28, 2026 - No ads, dark-mode and 6 months ... Ultimate to start with. In this quick tutorial, we’ll learn about the various ways of reading a file in Kotlin. We’ll cover both use cases of reading the entire file as a String, as well as reading it into a list of individual lines. Also obtaining it from a full absolute path or from a project resource...