Java Properties storeToXML()方法

java.util.Properties.storeToXML(OutputStream osString comment) 将属性写入writer对象以生成XML文档。

1 语法

public void storeToXML(OutputStream os,String comment)

2 参数

out:将要写出到XML文档的输出流。

comment:属性列表的描述;如果不需要评论,则为null。

3 返回值

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.Properties.storeToXML(OutputStream osString comment)方法的例子
 */
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

public class Demo {
    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>

热门文章

优秀文章