How to Store Properties into Xml File in Java


This example will show how to store properties into an Xml file. Here you will obtain an output streamĀ first (byte output stream only), and then use the StoreToXML() method in the Properties object to complete the saving of the content in Properties to the XML file.

Source Code

package com.beginner.examples;


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

public class StorePropertiesToXmlExample {

	public static void main(String[] args) {

		// Create a Properties collection
		Properties colorPro = new Properties();

		//Add something to this collection
		colorPro.setProperty("color_1", "red");
		colorPro.setProperty("color_2", "yellow");
		colorPro.setProperty("color_3", "blue");
		colorPro.setProperty("color_4", "black");
		colorPro.setProperty("color_5", "white");
		
		
		FileOutputStream os = null;
		
		try {
			os = new FileOutputStream("colorPro.xml");
			
			//Use the storeToXML() method in the Properties object to do this
			colorPro.storeToXML(os, null);
			
			//Close the resource
			os.close();
			
			System.out.println("Done");
			
		}catch(IOException e) {
			
			e.printStackTrace();
		}
		
	}

}

Output:

Done

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments