Using property files - I
16 May 2008To 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 …
Related Posts:
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