Wednesday, 3 May 2017

Java Tutorial: Java properties tutorial [How to store key-value pairs in XML with encoding]

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

/*
 * public void storeToXML(OutputStream os, String
 *                      comment, String encoding) 
 * 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.
 * 
 * encoding - the name of a supported character encoding
 */

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, using the
             * specified encoding.
             */
            p.storeToXML(fos, "Animal properties", "UTF-8");
            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_encoding_App.zip?attredirects=0&d=1

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/2010ece32ddd64329464f57cc3a56f059cb8f810/BasicJava/PropertiesDemo_storeToXML_encoding_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