Filesystem access • opensavvy.prepared.compat.filesystem.resources • resource
resource¶
fun resource(path: String, loader: ClassLoader): ResourceDesignator
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¶
fun resource(path: String, loader: Class<*>): ResourceDesignator
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:
The overload that takes a single parameter is a shorthand for this function.
See also¶
inline fun <TargetClass : Any> resource(path: String):
Loads a Java resource named path that is in the same package as TargetClass.
Example¶
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.