Monday, April 16, 2012

How to use java property files?


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?




Source: Tips4all

9 comments:

  1. 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);
    }

    ReplyDelete
  2. 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().

    ReplyDelete
  3. 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.

    ReplyDelete
  4. 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.

    ReplyDelete
  5. In order:


    You can store the file pretty much anywhere.
    no extension is necessary.
    Montecristo has illustrated how to load this. That should work fine.
    propertyNames() gives you an enumeration to iterate through.

    ReplyDelete
  6. 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.

    ReplyDelete
  7. 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"));

    ReplyDelete
  8. 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);
    }

    ReplyDelete
  9. **

    Properties pro = new Properties();
    FileInputStream in = new FileInputStream("D:/prop/prop.properties");
    pro.load(in);
    String temp1[];
    String temp2[];
    // getting values from property file
    String username = pro.getProperty("usernamev3");//key value in prop file
    String password = pro.getProperty("passwordv3");//eg. username="zub"
    String delimiter = ","; //password="abc"
    temp1=username.split(delimiter);
    temp2=password.split(delimiter);


    **

    ReplyDelete