Click Upvote Click Upvote - 1 year ago 73
Java Question

How to use Java property files?

I have a list of key/value pairs of configuration values I want to store as Java property files, and later load and iterate through.


  • Do I need to store the file in the same package as the class which will load them, or is there any specific location where it should be placed?

  • Does the file need to end in any specific extension or is

  • How can I load the file in the code

  • And how can I iterate through the values inside?

Zed Zed
Answer Source

You can pass an InputStream to the Property, so your file can pretty much be anywhere, and called anything.

Properties properties = new Properties();
try {
  properties.load(new FileInputStream("path/filename"));
} catch (IOException e) {

Iterate as:

for(String key : properties.stringPropertyNames()) {
  String value = properties.getProperty(key);
  System.out.println(key + " => " + value);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download