Monday, 6 March 2017

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

Click here to watch in Youtube :
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..");


Xml file is created successfully..

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

Click the below link to download the code:

Github Link:

Bitbucket Link:

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

    Google+ Followers

    Google+ Badge