How do I store properties as XML file?
Category: java.io, viewed: 1850 time(s).
In the previous example, How do I load properties from XML file? we read properties from XML file. Now it's the turn on how to store the properties as XML file.
package org.kodejava.example.io; import java.io.FileOutputStream; import java.util.Properties; public class PropertiesToXml { public static void main(String[] args) throws Exception { Properties properties = new Properties(); properties.setProperty("database.type", "mysql"); properties.setProperty("database.url", "jdbc:mysql://localhost/mydb"); properties.setProperty("database.username", "root"); properties.setProperty("database.password", "root"); FileOutputStream fos = new FileOutputStream("database-configuration.xml"); properties.storeToXML(fos, "Database Configuration", "UTF-8"); } } |
The saved XML file will look like the properties file below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Database Configuration</comment>
<entry key="database.password">root</entry>
<entry key="database.url">jdbc:mysql://localhost/mydb</entry>
<entry key="database.type">mysql</entry>
<entry key="database.username">root</entry>
</properties
Can't find what you are looking for? Join our FORUMS and ask some questions!
Related Examples
- How do I load properties from XML file?
- How do I convert InputStream to String?
- How do I convert string into InputStream?
- How do I get an exception stack trace message?
- How do I use Console class to read user input?
- How do I store objects in file?
- How do I use DataInputStream and DataOutputStream?
- How do I use RandomAccessFile class?
- How do I create a directories recursively?
- How can I get current working directory?