Java如何将属性存储为XML文件?
在上一个示例中,如何从XML文件加载属性?我们从XML文件读取属性。现在轮到如何将属性存储为XML文件了。
package org.nhooo.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");
}
}保存的XML文件将类似于以下属性文件:
<?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>
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短