Using property files - I

16 May 2008

To make your Java applications flexible, you might want to give properties that keep on changing in properties file. A property file is simply a text file that has key-value pairs relation.

If you are thinking of writing your parser to read the property files, then think again. Java provides java.util.Properties class, that will help you deal with propery files. Property files comprise of key-value pairs in a file, where the key and value are separated by an equal sign. Foe example:

mode=alert
packet=realTime

Now the idea is to load the property file into Properties object. Then you will havetwo keys (mode and packet) and two values (alert and realTime). The Properties class supports embedding Unicode strings with \u. Remember, everything is treated as a String.

Now lets see how to code it:

public class LoadSample {
  public static void main(String args[]) throws Exception {
    Properties prop = new Properties();
    FileInputStream fis = 
      new FileInputStream("sample.properties");
    prop.load(fis);
    prop.list(System.out);
    System.out.println("\nThe foo property: " +
        prop.getProperty("foo"));
  }
}

continued …

del.icio.us:Using property files - I  digg:Using property files - I  spurl:Using property files - I  wists:Using property files - I  simpy:Using property files - I  newsvine:Using property files - I  blinklist:Using property files - I  furl:Using property files - I  reddit:Using property files - I  fark:Using property files - I  blogmarks:Using property files - I  Y!:Using property files - I  smarking:Using property files - I  magnolia:Using property files - I  segnalo:Using property files - I  gifttagging:Using property files - I

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Using property files - I

Leave a Reply