当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java.util.Properties.storeToXML()用法及代码示例



描述

这个java.util.Properties.storeToXML(OutputStream osString comment,String encoding)方法使用指定的编码发出一个 XML 文档,该文档表示此表中包含的所有属性。

声明

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

public void storeToXML(OutputStream os,String comment, String encoding)

参数

  • out- 在其上发出 XML 文档的输出流。

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

返回值

此方法不返回值

异常

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

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

  • NullPointerException- 如果 out 为空。

示例

下面的例子展示了 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 and a different encoding
         prop.storeToXML(fos, "Properties Example","ISO 8859");

         // print the xml. Notice that ISO 8859 isn't supported
         while (fis.available() > 0) {
            System.out.print("" + (char) fis.read());
         }
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

Warning: The encoding 'ISO 8859' is not supported by the Java runtime.
Warning:encoding "ISO 8859" not supported, using UTF-8
<?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>

相关用法


注:本文由纯净天空筛选整理自 Java.util.Properties.storeToXML() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。