The usual way to access resource files is via the classpath, not by going directly to the filesystem.
There are several advantages to this:
- The resources will usually be deployed automatically, along with your compiled code; no need for manual copying.? (Maven and Gradle usually know to treat the
src/main/resources
folder this way.)
- If you build the application as a .jar/.war file, the resources will get included in that.? (Your code doesn't need to know or care that they're in an archive; as long as you access the resource as a stream, loading just works.)
- The application isn't tied to a particular directory layout or OS; it can run in any directory on any machine.
- You can override files.? Because the classpath can give several locations, a file in a later location can be overridden by adding a file into an earlier location.
It's easy enough to do: just call getResourceAsStream()
on the relevant classloader.? In this case, it could be as simple as:
val parser = javaClass.classLoader.getResourceAsStream("support/certain-file.json")?.use {
JsonFactory().createParser(it)
}
Here javaClass.classLoader
gets the current class's ClassLoader.? (You could use the system one instead, of course.)
use()
takes care of closing the resulting InputStream
once it's been read.
And luckily, there's a createParser()
method which accepts the InputStream
.
(Disclaimer: I don't know anything about Kubernetes, or about running Kotlin/Java programs on Windows.? But the beauty of using the classloader is that you don't need to!? If it can load classes, it can load resources too.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…