val fileContent = MySpec::class.java.getResource("/html/file.html").readText()
Answer from JB Nizet on Stack Overflowkotlin - How to read a text file from resources without javaClass - Stack Overflow
How to find resources in Kotlin? - Kotlin Discussions
Kotlin/Wasm: how to load a file from resources?
How to return a file from static folder using ktor?
How to read from a JSON file in Kotlin?
How do I open a Kotlin file?
How to use readln in Kotlin?
Videos
val fileContent = MySpec::class.java.getResource("/html/file.html").readText()
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")!!
Thanks to this answer for providing the correct way to read the file. Currently, reading files from resources without using javaClass or similar constructs doesn't seem to be possible.
// use this if you're inside a class
val lines = this::class.java.getResourceAsStream("file.txt")?.bufferedReader()?.readLines()
// use this otherwise
val lines = object {}.javaClass.getResourceAsStream("file.txt")?.bufferedReader()?.readLines()
According to other similar questions I've found, the second way might also work within a lambda but I haven't tested it. Notice the need for the ?. operator and the lines?.let {} syntax needed from this point onward, because getResourceAsStream() returns null if no resource is found with the given name.
Kotlin doesn't have its own means of getting a resource, so you have to use Java's method Class.getResource. You should not assume that the resource is a file (i.e. don't use toPath) as it could well be an entry in a jar, and not a file on the file system. To read a resource, it is easier to get the resource as an InputStream and then read lines from it:
val lines = this::class.java.getResourceAsStream("file.txt").bufferedReader().readLines()
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}")
}