Java.util.Properties.storeToXML() 方法

描述

java.util.Properties.storeToXML(OutputStream osString comment) 方法发出一个 XML 文档,表示此表中包含的所有属性。调用此方法的形式为 props.storeToXML(os, comment ) 的行为方式与调用 props.storeToXML(os, comment, "UTF-8"); 完全相同。


声明

以下是 java.util.Properties.storeToXML() 方法的声明

public void storeToXML(OutputStream os,String comment)

参数

  • out − 发出 XML 文档的输出流。

  • comments − 属性列表的描述,如果不需要注释,则返回 null。


返回值

该方法不返回值


异常

  • IOException − 如果将此属性列表写入指定的输出流会引发 IOException。

  • ClassCastException − 如果此 Properties 对象包含任何不是字符串的键或值。

  • NullPointerException − 如果 out 为 null。


示例

下面的例子展示了 java.util.Properties.storeToXML() 方法的使用。

package com.tutorialspoint;

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>