【发布时间】:2018-09-05 07:01:25
【问题描述】:
我编写了一个读取文本文件的代码。文本文件包含我想替换的占位符。替换不能以这种方式工作,并且字符串与占位符一起打印。这是我为此编写的代码:
class TestSub(val sub: Sub) {
fun create() = template()
fun template() = Files.newBufferedReader(ClassPathResource(templateId.location).file.toPath()).readText()
}
data class Sub(val name: String, val age: Int)
这是尝试打印最终字符串的主要函数:
fun main(args: Array<String>) {
val sub = Sub("Prashant", 32)
println(TestSub(sub).create())
}
但是,当我使用字符串而不是读取文件时,以下代码可以工作(替换 fun template())
fun template() = "<h1>Hello ${sub.name}. Your age is ${sub.age}</h1>"
有没有办法在读取文件内容时使字符串替换起作用?
【问题讨论】:
标签: kotlin