The Class java.util.Properties
Posted by Alberto - 21/06/09 at 11:06:58 pmI didnt know this class before start using it some months ago. Now I love it because it makes easier the handling of properties in your Java code.
public class Properties extends Hashtable {
protected Properties defaults;
public Properties();
public Properties(Properties defaults);
public String getProperty(String key);
public String setProperty(String key, String defaultValue);
public Enumeration propertyNames();
public void load(InputStream in) throws IOException;
public void save(OutputStream out, String header);
public void list(PrintStream out);}
It extends java.util.HashTable, so this class stores each property as a pair of (key, value). If your application needs some parameters to work, you can include them in a file, and then, use a java.util.Properties object to load them.
Properties props = new Properties();
FileInputStream file = new FileInputStream(”propertiesfile.txt”);
props.load(file)
Using these three lines, you will have a java.util.Properties object filled with the content of “propertiesfile.txt”. But, whats the format of this file? It has to contain a pair per line, and both of them have to be separated by a white space, ‘=” or ‘:’. The following file would be correct:
Property1=Value1
#This is a comment
Property2:Value2
Property3 Value3
After loading this file, how can we access the properties? If we want to use the value of “Property1″:
String value = getProperty(”Property1″);
Or if we want to modify the value of “Property2″:
setProperty(”Property2″,”New value”);
More info: java.util.Properties
Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds.
Valid XHTML and CSS.


