views:

3898

answers:

9

So 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.

Questions:

  • 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 .txt ok?

  • How can I load the file in the code

  • And how can I iterate through the values inside?

+1  A: 

This load the properties file:

Properties prop = new Properties();
InputStream stream = ...; //the stream to the file
try {
  prop.load(stream);
} finally {
  stream.close();
}

I use to put the .properties file in a directory where I have all the configuration files, I do not put it together with the class that accesses it, but there are no restrictions here.

For the name... I use .properties for verbosity sake, I don't think you should name it .properties if you don't want.

Alberto Zaccagni
However, some "extensions" of properties files assume the .properties extension - for example ResourceBundle used in I18N.
Nate
+1  A: 

By default, Java opens it in the working directory of your application (this behavior actually depends on the OS used). To load a file, do:

Properties props = new java.util.Properties();
FileInputStream fis new FileInputStream("myfile.txt");
props.load(fis)

As such, any file extension can be used for property file. Additionnaly, the file can also be stored anywhere, as long as you can use a FileInputStream.

On a related note if you use a modern framework, the framework may provide additionnal ways of opening a property file. For example, Spring provide a ClassPathResource to load a property file using a package name from inside a JAR file.

As for iterating through the properties, once the properties are loaded they are stored in the java.util.Properties object, which offer the propertyNames() method.

Thierry-Dimitri Roy
+6  A: 
  • You can store the file anywhere you like. If you want to keep it in your jar file, you'll want to use Class.getResourceAsStream() or ClassLoader.getResourceAsStream() to access it. If it's on the file system it's slightly easier.

  • Any extension is fine, although .properties is more common in my experience

  • Load the file using Properties.load, passing in an InputStream or a StreamReader if you're using Java 6. (If you are using Java 6, I'd probably use UTF-8 and a Reader instead of the default ISO-8859-1 encoding for a stream.)

  • Iterate through it as you'd iterate through a normal Hashtable (which Properties derives from), e.g. using keySet(). Alternatively, you can use the enumeration returned by propertyNames().

Jon Skeet
A: 

In order:

  1. You can store the file pretty much anywhere.
  2. no extension is necessary.
  3. Montecristo has illustrated how to load this. That should work fine.
  4. propertyNames() gives you an enumeration to iterate through.
Brian Agnew
+7  A: 

You can pass aenter code heren 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);
}
Zed
A: 
  • Ans1. Store the file in the same package.
  • Ans2. .properties extension.
  • Ans3. Using Properties, ListResourceBundle, ResourceBundle classes
  • Ans4. Using java iterators or enumerator

ResourceBundle class

 ResourceBundle rb=ResourceBundle.getBundle("prop"); // prop.properties
    System.out.println(rg.getString("key"));

Properties class

Properties ps=new Properties();
ps.Load(new java.io.FileInputStream("my.properties"));
adatapost
+2  A: 

If you put the properties file in the same package as class Foo, you can easily load it with

new Properties().load(Foo.class.getResourceAsStream("file.properties"))

Given that Properties extends Hashtable you can iterate over the values in the same manner as you would in a Hashtable.

If you use the *.properties extension you can get editor support, e.g. Eclipse has a properties file editor.

Fabian Steeg
You *can* do this - but I dislike storing properties files in the same package. You end up with properties files spread all over the place in your application. I'd much rather store all properties files in the root of the app, and load them as "class.getResourceAsStream("\file.properties")" or in some other known location.
Nate
Nate, that's true. However, in some scenarios the deployed location is not known (e.g. everything of your particular component is bundled into some archive). In such cases it can be quite convenient to say 'it's with that class, wherever that class ends up to be'. Also to avoid spreading the files all over, a single config package can be used for all the property files.
Fabian Steeg
Fabian, both of those cases work with my comment - it's based on the classpath - not the filesystem.
Nate
A: 

Here is another way to iterate over the properties:

Enumeration eProps = properties.propertyNames();
while (eProps.hasMoreElements()) { 
    String key = (String) eProps.nextElement(); 
    String value = properties.getProperty(key); 
    System.out.println(key + " => " + value); 
}
dertoni
mind pointing out the bug in Zed's answer?
leif81
I'm totally sorry. I reviewed the code in Zed's answer and it works quite well... I don't know what I thought back then...Actually his solution is nicer than mine, I think...
dertoni
A: 

and what if you want to modify some values and store them in the properties file. Using fileoutputstream seems to be creating a new file elsewhere. It does not make changes to the properties file loaded using input stream....any ideas? Thanks

fishinastorm