void storeToXML(OutputStream os, String comment)
描述 (Description)
java.util.Properties.storeToXML(OutputStream osString comment)方法发出一个XML文档,表示此表中包含的所有属性。对props.storeToXML(os,comment)形式的此方法的调用表现完全相同作为调用props.storeToXML(os,comment,“UTF-8”);.
声明 (Declaration)
以下是java.util.Properties.storeToXML()方法的声明
public void storeToXML(OutputStream os,String comment)
参数 (Parameters)
out - 要发出XML文档的输出流。
comments - 属性列表的描述,如果不需要注释,则为null。
返回值 (Return Value)
此方法不返回值
异常 (Exception)
IOException - 如果将此属性列表写入指定的输出流会抛出IOException。
ClassCastException - 如果此Properties对象包含任何不是字符串的键或值。
NullPointerException - 如果out为null。
例子 (Example)
以下示例显示了java.util.Properties.storeToXML()方法的用法。
package com.iowiki;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
public class PropertiesDemo {
public static void main(String[] args) {
Properties prop = new Properties();
// add some properties
prop.put("Height", "200");
prop.put("Width", "15");
try {
// create a output and input as a xml file
FileOutputStream fos = new FileOutputStream("properties.xml");
FileInputStream fis = new FileInputStream("properties.xml");
// store the properties in the specific xml
prop.storeToXML(fos, "Properties Example");
// print the xml
while (fis.available() > 0) {
System.out.print("" + (char) fis.read());
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Properties Example</comment>
<entry key="Width">15</entry>
<entry key="Height">200</entry>
</properties>