如何从 Kotlin 中的资源读取文本文件? [英] How to read a text file from resources in Kotlin?

查看:23
本文介绍了如何从 Kotlin 中的资源读取文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 Kotlin 编写 Spek 测试.测试应该从 src/test/resources 文件夹中读取一个 HTML 文件.怎么做?

I want to write a Spek test in Kotlin. The test should read an HTML file from the src/test/resources folder. How to do it?

class MySpec : Spek({

    describe("blah blah") {

        given("blah blah") {

            var fileContent : String = ""

            beforeEachTest {
                // How to read the file file.html in src/test/resources/html
                fileContent = ...  
            }

            it("should blah blah") {
                ...
            }
        }
    }
})

推荐答案

val fileContent = MySpec::class.java.getResource("/html/file.html").readText()

这篇关于如何从 Kotlin 中的资源读取文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆