Examples of what I may use this for could be the project name, email address, directory structures etc. I would not use ResourceBundlesfor constants.
Below is a code example;
File: project.properties
projectName=AnExampleProject
File: Properties.java
package example.properties;
import java.util.ResourceBundle;
public class Properties {
private static ResourceBundle rb = ResourceBundle.getBundle("example.properties.project");
public static String getProjectName() {
return rb.getString("projectName");
}
}
How it works
All properties are put into a .properties file which can be retrieved using ResourceBundle's API. We get a ResourceBundle object by using the ResourceBundle method getBundle, using the path to the properties file as a parameter. With that object, we can then retrieve the properties using the getString method (other types are also available e.g. getInt).
The methods are made static so the properties can be retrieved without instantiating the Properties class every time.
The project name would be retrieved using Properties.getProjectName();
No comments:
Post a Comment