Using OS independent paths on the JVM
Jun 23, 2017
1 minute read

Using plain path objects: Paths.get(URI.create("myPath")) Using file object (prefered in Kotlin with a bunch of extensions): File("myPath")

Loading resources:

fun resource(name: String): URI {
    val explicitName = if (name.startsWith("/")) name else "/$name"
    val resource = Resources::class.java.getResource(explicitName)
    requireNotNull(resource) { "Make sure the resource '$name' exists!" }
    return resource.toURI()
}

fun resourceAsStream(name: String): InputStream = Files.newInputStream(Paths.get(resource(name)))

fun resourceAsString(name: String): String = String(Files.readAllBytes(Paths.get(resource(name))))