resource
Loads a Java resource named path from loader.
The resource will be accessed using ClassLoader.getResource, ClassLoader.getResourceAsStream, etc. That is to say, the resource should be stored in the root package of the loader.
See also
Loads a Java resource named path from loader.
The resource will be accessed using Class.getResource, Class.getResourceAsStream, etc. That is to say, the resource should be stored in the same package as the given class.
Example
For this code to work:
// File TestClass.kt
package foo.bar.baz
object TestClass
val test by resource("test.txt", TestClass::class)
.read()
the test.txt
file should be placed in the exact same package as the TestClass
. With typical Gradle configuration, the project may look like:
src/
main/
kotlin/
foo/
bar/
baz/
TestClass.kt
resource/
foo/
bar/
baz/
test.txt
The overload that takes a single parameter is a shorthand for this function.
See also
Loads a Java resource named path that is in the same package as TargetClass.
Example
package foo.bar.baz
object TargetClass
val test by resource<TargetClass>("test.txt")
.read()
For this example to work, the file test.txt
should be in the same package as the TargetClass
, meaning in the resources in subfolder foo/bar/baz/test.txt
.
To learn more about the location algorithm, see the overload that accepts a Class.