JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to store property file as xml file?


Description:

We can also create property files in xml format dynamically. First creat Properties object, add all property key-value pair by using setProperty() method. Then write this file to FileOutputStream object to store is as a property file as xml. Call storeToXML() method to store it to the file system. Below example shows how to create it.


Code:
package com.java2novice.files;

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("MyProp.xml");
			prop.storeToXML(os, "Dynamic Property File");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
	}
}

Output:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<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 >>

Java File IO Operations Sample Code Examples

  1. All file operations.
  2. Show list of all file names from a folder.
  3. How to get list of all files from a folder?
  4. Filter the files by file extensions and show the file names.
  5. How to read file content using byte array?
  6. How to read file content line by line in java?
  7. How to read property file in static context?
  8. How to read input from console in java?
  9. How to get file list from a folder filtered by extensions?
  10. How to get file URI reference?
  11. How to store and read objects from a file?
  12. How to create and store property file dynamically?
  13. How to store property file as xml file?
  14. How to get file last modified time?
  15. How to convert byte array to inputstream?
  16. How to convert inputstream to reader or BufferedReader?
  17. How to convert byte array to reader or BufferedReader?
  18. How to set file permissions in java?
  19. How to read a file using BufferedInputStream?
  20. How to create temporary file in java?
  21. How to write or store data into temporary file in java?
  22. How to delete temporary file in java?
  23. How to write string content to a file in java?
  24. How to write byte content to a file in java?
Knowledge Centre
Can we call servlet destory() from service()?
As you know, destory() is part of servlet life cycle methods, it is used to kill the servlet instance. Servlet Engine is used to call destory(). In case, if you call destory method from service(), it just execute the code written in the destory(), but it wont kill the servlet instance. destroy() will be called before killing the servlet instance by servlet engine.
Famous Quotations
We know what we are, but know not what we may be.
-- William Shakespeare

About Author

Most Visited Pages

Other Interesting Sites

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