Java language specification provides ‘Properties’ class which can be used to access the string properties of the application. This class can facilitate the central storage for application’s configuration information. While using this class in the application, we can load the properties from the text file having ‘.properties’ extension. Properties file contains <key, value> pairs, where each key and value are Strings individually.

Properties files are also used in internationalization\Localization of the application where the keys are common in all properties files each corresponding to different locales\languages and the string values are defined in respective language.

Many a times it becomes necessary to represent this properties information in XML format especially while performing data sending and receiving operations. ‘java.util.Properties’ class has got the methods to perform the properties to XML conversions and vice versa. Let us see an example code which does properties to XML conversion:

package tutorials;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

/**
* @author User
*
*/
public class PropsToXML {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Properties props = new Properties();
try {
props.load(PropsToXML.class.getClassLoader().getResourceAsStream(“tutorials/MyResources.properties”));
File xmlFile = new File(“xmlProps.xml”);
xmlFile.createNewFile();
OutputStream out = new FileOutputStream(xmlFile);
props.storeToXML(out, “Properties to XML”, “UTF-8″);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

MyResources.properties file is in the same package ‘tutorials’, with the contents:

locale.info=English US
locale.currency=Dollar
locale.timezone=EST

When the above program is run ‘xmlProps.xml’ file is created with the following contents:

Properties to XML
Dollar
EST
English US

Similarly, the same class provides another method which is useful in converting the xml data into (key, value) properties. Following code depicts the same:

package tutorials;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

/**
* @author User
*
*/
public class XMLToProps {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Properties props = new Properties();
InputStream in;
try {
in = new FileInputStream(“xmlProps.xml”);
props.loadFromXML(in);

System.out.println(props.get(“locale.timezone”));
System.out.println(props.get(“locale.currency”));
System.out.println(props.get(“locale.info”));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidPropertiesFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

The xml input file contents are as given above and the output of the above code :

EST
Dollar
English US

Related Posts

Properties file to XML and vice versa conversion using Java program admin Core Java
Java language specification provides 'Properties' class which can be used to access the string properties of the application. This class can facilitate the central storage for application's configuration information. While using this class in the application, we can load the properties from the text file having '.properties' extension. Properties...
Java language specification provides 'Properties' class which can be used to access the string properties of the application. This class can facilitate the central storage for application's configuration information. While using this class in the application, we can <a title="How to read properties file using Java?" href="http://www.javaexperience.com/how-to-read-properties-file-using-java/">load the properties from the text file</a> having '.properties' extension. Properties file contains <key, value> pairs, where each key and value are Strings individually. Properties files are also used in internationalization\<a title="Adding Localization support to the Java based applications" href="http://www.javaexperience.com/adding-localization-support-to-the-java-based-applications/">Localization of the application</a> where the keys are common in all properties files each corresponding to different locales\languages and the string values are defined in respective language. Many a times it becomes necessary to represent this properties information in XML format especially while performing data sending and receiving operations. 'java.util.Properties' class has got the methods to perform the properties to XML conversions and vice versa. Let us see an example code which does properties to XML conversion: 1 package tutorials; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Properties; /** * @author User * */ public class PropsToXML { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Properties props = new Properties(); try { props.load(PropsToXML.class.getClassLoader().getResourceAsStream("tutorials/MyResources.properties")); File xmlFile = new File("xmlProps.xml"); xmlFile.createNewFile(); OutputStream out = new FileOutputStream(xmlFile); props.storeToXML(out, "Properties to XML", "UTF-8"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 1 MyResources.properties file is in the same package 'tutorials', with the contents: <blockquote>locale.info=English US locale.currency=Dollar locale.timezone=EST</blockquote> When the above program is run 'xmlProps.xml' file is created with the following contents: 1 <!--?xml version="1.0" encoding="UTF-8" standalone="no"?--> Properties to XML Dollar EST English US 1 Similarly, the same class provides another method which is useful in converting the xml data into (key, value) properties. Following code depicts the same: 1 package tutorials; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.InvalidPropertiesFormatException; import java.util.Properties; /** * @author User * */ public class XMLToProps { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Properties props = new Properties(); InputStream in; try { in = new FileInputStream("xmlProps.xml"); props.loadFromXML(in); System.out.println(props.get("locale.timezone")); System.out.println(props.get("locale.currency")); System.out.println(props.get("locale.info")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidPropertiesFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 1 The xml input file contents are as given above and the output of the above code : <blockquote>EST Dollar English US</blockquote>
What is Timer and TimerTask in Java
Compress and Decompress zip files using Java
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.

Latest posts by admin (see all)

Comments

comments