Properties class:
Properties class is used to maintain the data in the key-value form. It takes both key and value as a string. Properties class is a subclass of Hashtable. It provides the methods to store properties in a properties file and to get the properties from the properties file. System.getProperties() returns the all system properties.
Properties class Constructor:
Method | Description |
Properties() | It will create an empty property list with no default values. |
Properties(Properties defaults) | It will create an empty property list with the specified defaults. |
Properties class Methods:
Method | Description |
public void load(Reader r) | It will load data from the Reader object. |
public void load(InputStream is) | It will load data from the InputStream object |
public void loadFromXML(InputStream in) | It will load all of the properties represented by the XML document on the specified input stream into this properties table. |
public String getProperty(String key) | It will return value based on the key. |
public String getProperty(String key, String defaultValue) | It will search for the property with the specified key and return the value. |
public void setProperty(String key, String value) | It will call the put method of Hashtable. |
public void list(PrintStream out) | It will print the property list out to the specified output stream. |
public void list(PrintWriter out) | It will print the property list out to the specified output stream. |
public Enumeration<?> propertyNames() | It will return an enumeration of all the keys from the property list. |
public Set<String> stringPropertyNames() | It will return a set of keys from the property list where the key and its corresponding value are strings. |
public void store(Writer w, String comment) | It will write the properties in the writer object. |
public void store(OutputStream os, String comment) | It will write the properties in the OutputStream object. |
public void storeToXML(OutputStream os, String comment) | It will write the properties in the writer object for generating XML documents. |
public void storeToXML(Writer w, String comment, String encoding) | It will write the properties in the writer object for generating XML documents with the specified encoding. |
Properties class example:
PropertiesTest.java
import java.util.Iterator; import java.util.Properties; import java.util.Set; /** * This class is used to show the simple * Properties class example. * @author w3schools */ public class PropetiesTest { public static void main(String[] args) { Properties prop = new Properties(); Set props; String str; //Set the properties value prop.setProperty("database", "oracle10g"); prop.setProperty("username", "system"); prop.setProperty("password", "oracle"); //Get key set props = prop.keySet(); //Print the properties Iterator iterator = props.iterator(); while(iterator.hasNext()) { str = (String) iterator.next(); System.out.println("Prpperty: " + str + ", Property Value: " + prop.getProperty(str)); } } } |
Output:
Prpperty: password, Property Value: oracle Prpperty: database, Property Value: oracle10g Prpperty: username, Property Value: system |
Properties class example to write properties in a properties file and read it:
PropertiesTest.java
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Iterator; import java.util.Properties; import java.util.Set; /** * This class is used to write properties in a properties * file and read it. * @author w3schools */ public class PropetiesTest { public static void main(String[] args) { Properties prop = new Properties(); Set props; String str; //Set the properties value prop.setProperty("database", "oracle10g"); prop.setProperty("username", "system"); prop.setProperty("password", "oracle"); //Save properties to the project root folder try { OutputStream output = new FileOutputStream("info.properties"); prop.store(output, null); output.close(); } catch (IOException e) { e.printStackTrace(); } //Load the properties file Properties infoProp = new Properties(); try { InputStream input = new FileInputStream("info.properties"); infoProp.load(input); input.close(); } catch (IOException e) { e.printStackTrace(); } //Get key set props = infoProp.keySet(); //Print the properties Iterator iterator = props.iterator(); while(iterator.hasNext()) { str = (String) iterator.next(); System.out.println("Prpperty: " + str + ", Property Value: " + infoProp.getProperty(str)); } } } |
Output:
Prpperty: password, Property Value: oracle Prpperty: database, Property Value: oracle10g Prpperty: username, Property Value: system |
Properties class example to get system properties:
PropertiesTest.java
import java.util.Iterator; import java.util.Properties; import java.util.Set; /** * This class is used to get the system properties * using Properties class. * @author w3schools */ public class PropetiesTest { public static void main(String[] args) { //Get system properties. Properties prop = System.getProperties(); Set props; String str; //Get key set props = prop.keySet(); //Print the properties Iterator iterator = props.iterator(); while(iterator.hasNext()) { str = (String) iterator.next(); System.out.println("Prpperty: " + str + ", Property Value: " + prop.getProperty(str)); } } } |
Output:
Prpperty: java.runtime.name, Property Value: Java(TM) SE Runtime Environment Prpperty: sun.boot.library.path, Property Value: C:\Program Files (x86)\Java\jre7\bin Prpperty: java.vm.version, Property Value: 23.25-b01 Prpperty: java.vm.vendor, Property Value: Oracle Corporation Prpperty: java.vendor.url, Property Value: http://java.oracle.com/ Prpperty: path.separator, Property Value: ; Prpperty: java.vm.name, Property Value: Java HotSpot(TM) Client VM Prpperty: file.encoding.pkg, Property Value: sun.io Prpperty: user.country, Property Value: IN Prpperty: user.script, Property Value: Prpperty: sun.java.launcher, Property Value: SUN_STANDARD ... |
Next Topic: Hashtable in java with example.
Previous Topic: Comparator interface in java with example.