JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to store property file as xml file?


Description:

This example shows how to get create properties file dynamically and store as xml file. You have to create Properties object, set all properties by using setProperty() method, store it to file system as xml file by using storeToXML() method.


Code:
package com.java2novice.properties;

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

public class MyFileXmlStore {
	
	public static void main(String a[]) throws IOException{
		
		OutputStream os = null;
		Properties prop = new Properties();
		prop.setProperty("name", "java2novice");
		prop.setProperty("domain", "www.java2novice.com");
		prop.setProperty("email", "[email protected]");
		try {
			os = new FileOutputStream("C:/MyProp.xml");
			prop.storeToXML(os, "Dynamic Property File");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
	}
}

Output:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Dynamic Property File</comment>
<entry key="email">[email protected]</entry>
<entry key="name">java2novice</entry>
<entry key="domain">www.java2novice.com</entry>
</properties>
<< Previous Program | Next Program >>

List of Properties class sample programs:

  1. How to load Properties file from a file system?
  2. How to load Properties file from the classpath?
  3. How to load Properties file from a static block or static method?
  4. How to assign default values for unavailable keys in properties file?
  5. How to get all keys from properties file?
  6. How to create and store property file dynamically?
  7. How to store property file as xml file?
  8. How to load property file using class name in java?
Knowledge Centre
What is wrapper class?
Everything in java is an object, except primitives. Primitives are int, short, long, boolean, etc. Since they are not objects, they cannot return as objects, and collection of objects. To support this, java provides wrapper classes to move primitives to objects. Some of the wrapper classes are Integer, Long, Boolean, etc.
Famous Quotations
Education is what remains after one has forgotten what one has learned in school.
-- Albert Einstein

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.