Friday, January 9, 2009

Using java properties

Properties

Using the java.util.Properties API, properties(key=value pairs) can be read from files. The properties can be listed, modified and can be saved to files. An example is shown below.

public class MyFileProperties {
public static final String appPropFile = "Props";
public static void main(String[] args) throws IOException{
FileInputStream fs = null;
FileOutputStream fos= null;

try {

//Load application's properties from a file
fs = new FileInputStream(appPropFile);
Properties myProps = new Properties();
myProps.load(fs);

//Check if the application has any properties
System.out.println(myProps.isEmpty());

//Add new properties to the application
myProps.setProperty("application.name","My Properties");
myProps.setProperty("application.version","1.0");

//List the properties as key=value pairs
myProps.list(System.out);

//Print only keys of properties
Set<String> s = myProps.stringPropertyNames();
String[] properties = s.toArray(new String[0]);
for (String prop: properties){
System.out.println("Property Name: " + prop);
}

//Save the application properties back to the file
fos = new FileOutputStream(appPropFile);
myProps.store(fos, "last updated " + new java.util.Date());

} catch (FileNotFoundException fe) {
fe.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
} finally{
if (fos != null) fos.close();
if (fs != null) fs.close();
}

}

}


The output is shown below:
false
-- listing properties --
nikki=akku
application.name=My Properties
application.version=1.0
hello=world
application.release=7
ann=kutty
Property Name: nikki
Property Name: application.name
Property Name: application.version
Property Name: hello
Property Name: application.release
Property Name: ann

The properties file is shown below:
#last updated Tue Mar 17 00:56:04 PDT 2009
#Tue Mar 17 00:56:05 PDT 2009
nikki=akku
application.name=My Properties
application.version=1.0
hello=world
application.release=7
ann=kutty

Properties and XML files

JDK 1.5 introduced APIs to load and store properties from/to XML files. The document must satisfy the DTD described here. An example is shown below.



public class MyXMLProperties {
public static void main(String[] args) throws IOException{
FileInputStream fx = null;
FileOutputStream fox = null;

try {
Properties myProps = new Properties();

//Load properties from an XML file
fx = new FileInputStream("releasexml.xml");
myProps.loadFromXML(fx);

//Add new properties to the application
myProps.setProperty("application.name","My Properties");

//List the properties as key=value pairs
myProps.list(System.out);

//Save the properties to a XML file
fox = new FileOutputStream("XMLProps.xml");
myProps.storeToXML(fox, null);

} catch (FileNotFoundException fe) {
fe.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
} finally{
if (fox != null) fox.close();
if (fx != null) fx.close();
}
}
}

The output is shown below:

-- listing properties

application.name=My Properties

application.release=7


The XML file is shown below:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
< properties>
< entry key="application.name"> My Properties</entry>
< entry key="application.release"> 7</entry>
</properties>

System Properties

Using the getProperty() or the getProperties() method of java.lang.System one can get the properties of the OS and Java. Also new system properties can be added or existing ones can be deleted. If one does not have the permissions, a security exception could be thrown at runtime. An example is shown below.


public class MySystemProperties {

public static void main(String[] args) {

//Get the system properties
Properties sysProp = System.getProperties();

//Print all the system properties
sysProp.list(System.out);

//Get a system property.
String javaVersion = System.getProperty("java.version");
System.out.println("Java Version is " + javaVersion);

//Add a system property
System.setProperty("admin.name", "foobar");
System.out.println("Admin name is " + System.getProperty("admin.name"));

//Delete a system property, need to have privileges to do this
System.clearProperty("admin.name");
System.out.println("Admin name is " + System.getProperty("admin.name"));

}

}


The output is shown below:

-- listing properties --
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=C:\Program Files\Java\jre1.6.0_04\bin
.........

sun.desktop=windows
sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+m...
Java Version is 1.6.0_04
Admin name is foobar
Admin name is null

No comments:

Post a Comment