val fileContent = MySpec::class.java.getResource("/html/file.html").readText()
Answer from JB Nizet on Stack Overflow
🌐
Kotlin Discussions
discuss.kotlinlang.org › language design
What's the intended way to read a resource file? - Language Design - Kotlin Discussions
December 15, 2021 - 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").readLines() // only works inside a class val lines = this::class.java.getResourceAsStream("file.txt")?.bufferedReader()?.readLines() // works otherwise val lines = object {}.javaClass.getResourceAsStream("file.txt")?.bufferedReader()?.readLines() Admittedly, none of the thr...
🌐
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 - When using getResource, the passed in fileName is not an absolute file name but a name relative to our project resources. Finds a resource with the given name and returns an InputStream instance: fun readFileAsLinesUsingGetResourceAsStream(fileName: String) = this::class.java.getResourceAsStream(fileName).bufferedReader().readLines()
Discussions

kotlin - How to read a text file from resources without javaClass - Stack Overflow
I need to read a text file with readLines() and I've already found this question, but the code in the answers always uses some variation of javaClass; it seems to work only inside a class, while I'm More on stackoverflow.com
🌐 stackoverflow.com
How to find resources in Kotlin? - Kotlin Discussions
I thought I'd find the answer to this, and indeed my code compiles, but I eventually get a runtime error. Here’s some simple code that tries to create a JavaFX UI from an FXML specification (a resource): package ui import kotlinfx.builders.* import kotlinfx.properties.* import javafx.app... More on discuss.kotlinlang.org
🌐 discuss.kotlinlang.org
0
March 8, 2015
Kotlin/Wasm: how to load a file from resources?
Thank you very much for publishing the solution. It has been a great help. More on reddit.com
🌐 r/Kotlin
2
5
December 15, 2023
How to return a file from static folder using ktor?
fun getResource(path: String) = object{}.javaClass.getResource(path) More on reddit.com
🌐 r/Kotlin
10
5
October 8, 2023
People also ask

How to read from a JSON file in Kotlin?
To read a JSON file in Kotlin, use libraries like Kotlinx Serialization, Gson, or Moshi. First, read the file content as a string, then parse it into a Kotlin object using the chosen library's deserialization features.
🌐
dhiwise.com
dhiwise.com › post › simplifying-kotlin-read-from-file-a-practical-approach
Kotlin Read From File: Step-by-Step Instructions Simplified
How do I open a Kotlin file?
To open a Kotlin file programmatically, use the File class from `java.io`. Specify the file's path and utilize methods like `readText()` or `bufferedReader()` to access its content. Ensure the file path is correct and accessible.
🌐
dhiwise.com
dhiwise.com › post › simplifying-kotlin-read-from-file-a-practical-approach
Kotlin Read From File: Step-by-Step Instructions Simplified
How to use readln in Kotlin?
The `readln()` function is used to read input from the console, not from files. It captures a single line of input as a string. For file reading, use methods like`File.readText()` or BufferedReader.
🌐
dhiwise.com
dhiwise.com › post › simplifying-kotlin-read-from-file-a-practical-approach
Kotlin Read From File: Step-by-Step Instructions Simplified
🌐
ZetCode
zetcode.com › kotlin › readfile
Kotlin read file - reading file in Kotlin
January 29, 2024 - package com.zetcode import java.io.File import java.io.InputStream import java.nio.charset.Charset fun main() { val fileName = "src/resources/thermopylae.txt" val myFile = File(fileName) var ins: InputStream = myFile.inputStream() var content = ins.readBytes().toString(Charset.defaultCharset()) println(content) } The example creates an InputStream from a File and reads bytes from it.
🌐
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
Find elsewhere
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 4404783072914-How-to-read-resources-using-kotlin
How to read resources using kotlin – IDEs Support (IntelliJ Platform) | JetBrains
August 6, 2021 - I'm new to kotlin, creating plugin using Kotlin. I have properties file added to resources folder, want to read proprerties file using kotlin Project: I tried the same using java, i was able to ...
🌐
DhiWise
dhiwise.com › post › simplifying-kotlin-read-from-file-a-practical-approach
Kotlin Read From File: Step-by-Step Instructions Simplified
January 21, 2025 - Its seamless interoperability with Java libraries means you can use familiar java.io classes alongside Kotlin-specific utilities to read file content efficiently. Kotlin also helps you avoid common pitfalls, like resource leaks, by making it easy to automatically close streams and readers. Before diving into the code, ensure your Kotlin project is configured correctly. You’ll often need to work with classes like BufferedReader or InputStream from Java.
🌐
YouTube
youtube.com › luke chaffey
How to read a text file from resources in Kotlin? - YouTube
kotlin: How to read a text file from resources in Kotlin?Thanks for taking the time to learn more. In this video I'll go through your question, provide vario...
Published   October 23, 2023
Views   74
🌐
Kotlin Discussions
discuss.kotlinlang.org › language design
What's the intended way to read a resource file? - #3 by broot - Language Design - Kotlin Discussions
December 15, 2021 - 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…
🌐
Kotlinlang
slack-chats.kotlinlang.org › t › 12918338 › hi-i-want-to-read-a-json-file-from-a-resource-folder-in-comm
Hi I want to read a json file from a resource folder in comm kotlinlang #multiplatform
@Adam S yes generating a file with ... limited number of targets then you should be able to expect/actual some custom · loadResource() function, so long as you know how to load a resource for each specific target?...
🌐
Kotlin Discussions
discuss.kotlinlang.org › language design
What's the intended way to read a resource file? - #2 by al3c - Language Design - Kotlin Discussions
December 15, 2021 - 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…
🌐
Kotlinlang
slack-chats.kotlinlang.org › t › 528113 › is-it-possible-to-read-files-from-the-resource-folder-from-k
Is it possible to read files from the `resource` folder from kotlinlang #kotlin-native
April 30, 2020 - cwd for my tests, which is not the same but works good enough: https://github.com/HearthSim/Arcane-Tracker/blob/master/kotlin-hsreplay-api/src/macosX64Test/kotlin/net/hearthsim/hsreplay/expected.kt#L16 ... If you want to do that in the final executable, I guess it's a packaging problem, I'm not sure you can embed resources in native binaries easily
🌐
BezKoder
bezkoder.com › home › how to read file in kotlin
How to read File in Kotlin - BezKoder
February 16, 2020 - So create a file named bezkoder.txt ... 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")...
🌐
Devgex
devgex.com › en › article › 00031447
Complete Guide to Reading Text Files from Resources in Kotlin - DevGex
December 1, 2025 - In the Java and Kotlin ecosystems, reading resource files from the classpath is a standard operational pattern.
🌐
YouTube
youtube.com › watch
Read Files from Resources in Android | Kotlin Tutorial - YouTube
Read Files from Resources in Android | Kotlin Tutorial 📂 How to Read Files from Resources in AndroidIn this video, you’ll learn how to access and read files...
Published   February 18, 2025
🌐
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 · 中文 – 简体
🌐
Kotlin Discussions
discuss.kotlinlang.org › t › how-to-find-resources-in-kotlin › 315
How to find resources in Kotlin? - Kotlin Discussions
March 8, 2015 - I thought I'd find the answer to this, and indeed my code compiles, but I eventually get a runtime error. Here’s some simple code that tries to create a JavaFX UI from an FXML specification (a resource): package ui import kotlinfx.builders.* import kotlinfx.properties.* import javafx.application.Application import javafx.stage.Stage import javafx.fxml.FXMLLoader fun main(args: Array ) { Application.launch(javaClass ()) } class LaRGUI : Application() { override fun s...
🌐
Reddit
reddit.com › r/kotlin › kotlin/wasm: how to load a file from resources?
r/Kotlin on Reddit: Kotlin/Wasm: how to load a file from resources?
December 15, 2023 -

In the KMP Wizzard WEB project, how can I load the content of a JSON file?

[SOLVED]

//resources/index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">

<title>Compose App</title>

<script>

    let jsonData;

    function getData() {
        return JSON.stringify(jsonData);
    }
    fetch('data.json')
        .then(response => response.json())
        .then(data => {
            jsonData = data;
        })
        .catch(error => console.error('Error:', error));
</script>
<script type="application/javascript" src="skiko.js"></script>
<script type="application/javascript" src="composeApp.js"></script>
</head>
<body>
<canvas id="ComposeTarget"></canvas>
</body>
</html>

//C:\MyProject\composeApp\src\wasmJsMain\kotlin\main.kt

import kotlinx.serialization.json.Json
import kotlinx.serialization.Serializable

@Serializable
data class MyObject(
    val anInteger: Int?,
    val someText: String?
)

external fun getData(): String

fun main() {
    val jsonString = getData()
    val jsonObject: MyObject = Json.decodeFromString(jsonString)
    println("anInteger: ${jsonObject.anInteger}")
    println("someText: ${jsonObject.someText}")
}