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 three ways looks perfect and I’m wondering if there’s something I’m missing, or maybe if a better workaround is already planned. You can also refer to this SO question I’ve posted earlier.