Monday 6 March 2017

Java Tutorial: Properties class in java [How to store key-value pairs in xml]


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=9QvNyqi2xGs&list=UUhwKlOVR041tngjerWxVccw

PropertiesDemo.java
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/*
 * public void storeToXML(OutputStream os, String
 *                              comment) throws IOException
 * 
 * Parameters: 
 * -----------
 * 
 * os - the output stream on which to emit the XML
 * document. 
 * 
 * comment - a description of the property list, 
 * or null if no comment is desired.
 */

class PropertiesDemo
{

    public static void main(String[] args) throws IOException
    {
        Properties p = new Properties();
        // add some properties
        p.put("name", "Dog");
        p.put("age", "2 years");

        try (FileOutputStream fos = new FileOutputStream("properties.xml"))
        {
            /*
             * Emits an XML document representing all of the
             * properties contained in this table.
             */
            p.storeToXML(fos, "Animal properties");
            System.out.println("Xml file is created successfully..");

        }

    }
}
Output
Xml file is created successfully..

properties.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Animal properties</comment>
<entry key="age">2 years</entry>
<entry key="name">Dog</entry>
</properties>

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/PropertiesDemo_storeToXML_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/PropertiesDemo_storeToXML_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/2010ece32ddd64329464f57cc3a56f059cb8f810/BasicJava/PropertiesDemo_storeToXML_App/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment