How to read a text file from resources in Kotlin?

后端 未结 10 563
心在旅途
心在旅途 2020-12-08 17:59

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({         


        
10条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 18:26

    Kotlin + Spring way:

    @Autowired
    private lateinit var resourceLoader: ResourceLoader
    
    fun load() {
        val html = resourceLoader.getResource("classpath:html/file.html").file
            .readText(charset = Charsets.UTF_8)
    }
    

提交回复
热议问题