Let’s say I have a structure
-bin/com/abc/A.class
-src/com/abc/A.java
-config/info.txt
How to address the file info.txt from A class?
Should we use “user.dir” property or “config/info.txt” so that it would work ?
I’ll compile this into the jar and after that
the jar will be used from the servlet,
but I don’t think that’s important
cause this file is written and read from
internal jar’s methods only.
Just put it in the runtime classpath and use
ClassLoader#getResourceAsStream()to get anInputStreamof it. Putting it in the JAR file among the classes, or adding its (JAR-relative) path to theClass-Pathentry of the JAR’smanifest.mffile is more than sufficient.Or if you actually want to get it in flavor of a
java.io.File, then make use ofClassLoader#getResource(),URL#toURI()and theFileconstructor taking anURI:Do not use relative paths in
java.iostuff. It would be dependent on the current working directory which you have no control over at any way. It’s simply receipt for portability trouble. Just make use of the classpath.That said, are you aware of the
java.util.PropertiesAPI? It namely look like you’re trying to achieve the same thing which is more easy to be done with propertiesfiles.