Loading Properties File from Java Package
Loading properties files buried within package structures can be a challenge, especially when seeking independence from servlet containers. To load a properties file from a package, consider the following:
Loading Properties from Within the Package:
To load properties from a file within the same package (com.al.common.email.templates), use the following approach:
<code class="java">Properties prop = new Properties(); InputStream in = getClass().getResourceAsStream("foo.properties"); prop.load(in); in.close();</code>
Exception Handling
Remember to handle any necessary exceptions while loading the properties.
Loading Properties Outside the Package:
If your class is not within the specified package, adjust the input stream acquisition:
<code class="java">InputStream in = getClass().getResourceAsStream("/com/al/common/email/templates/foo.properties");</code>
Relative Paths and Absolute Paths:
The above is the detailed content of How to Load Properties Files from a Package in Java?. For more information, please follow other related articles on the PHP Chinese website!