Java Properties storeToXML(OutputStream os, String comment) 方法



描述

該 **Java Properties storeToXML(OutputStream os, String comment)** 方法會輸出一個 XML 文件,該文件表示此表中包含的所有屬性。 呼叫此方法的形式為 `props.storeToXML(os, comment)`,其行為與呼叫 `props.storeToXML(os, comment, "UTF-8")` 完全相同。

宣告

以下是 **Java Properties storeToXML()** 方法的宣告

public void storeToXML(OutputStream os,String comment)

引數

  • **out** − 用於輸出 XML 文件的輸出流。

  • **comments** − 屬性列表的描述,如果不需要註釋,則為 null。

返回值

此方法不返回值

異常

  • **IOException** − 如果將此屬性列表寫入指定的輸出流引發 IOException。

  • **ClassCastException** − 如果此 Properties 物件包含任何非字串的鍵或值。

  • **NullPointerException** − 如果 out 為 null。

Java Properties storeToXML(OutputStream os, String comment,String encoding) 方法

描述

該 **java.util.Properties.storeToXML(OutputStream os, String comment, String encoding)** 方法使用指定的編碼輸出一個 XML 文件,該文件表示此表中包含的所有屬性。

宣告

以下是 **java.util.Properties.storeToXML()** 方法的宣告

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

引數

  • **out** − 用於輸出 XML 文件的輸出流。

  • **comments** − 屬性列表的描述,如果不需要註釋,則為 null。

  • **encoding** − 受支援的字元編碼的名稱。

返回值

此方法不返回值

異常

  • **IOException** − 如果將此屬性列表寫入指定的輸出流引發 IOException。

  • **ClassCastException** − 如果此 Properties 物件包含任何非字串的鍵或值。

  • **NullPointerException** − 如果 os 或 encoding 為 null。

  • **UnsupportedEncodingException** − 如果實現不支援該編碼。

Java Properties storeToXML(OutputStream os, String comment,Charset encoding) 方法

描述

該 **java.util.Properties.storeToXML(OutputStream os, String comment, Charset encoding)** 方法使用指定的編碼輸出一個 XML 文件,該文件表示此表中包含的所有屬性。

宣告

以下是 **java.util.Properties.storeToXML()** 方法的宣告

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

引數

  • **out** − 用於輸出 XML 文件的輸出流。

  • **comments** − 屬性列表的描述,如果不需要註釋,則為 null。

  • **charset** − 字元集。

返回值

此方法不返回值

異常

  • **IOException** − 如果將此屬性列表寫入指定的輸出流引發 IOException。

  • **ClassCastException** − 如果此 Properties 物件包含任何非字串的鍵或值。

  • **NullPointerException** − 如果 os 或 charset 為 null。

將 Properties 條目儲存到 XML 檔案的示例

以下示例演示瞭如何使用 Java Properties storeToXML(OutputStream os, String comment) 方法將屬性物件儲存到 xml 檔案中,然後列印 xml。

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

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"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Properties Example</comment>
<entry key="Height">200</entry>
<entry key="Width">15</entry>
</properties>

使用給定編碼將 Properties 條目儲存到 XML 檔案的示例

以下示例演示瞭如何使用 Java Properties storeToXML(OutputStream os, String comment, String encoding) 方法使用給定編碼將屬性物件儲存到 xml 檔案中,然後列印 xml。

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

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","windows-1252");

         // 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();
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

<?xml version="1.0" encoding="windows-1252"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Properties Example</comment>
<entry key="Height">200</entry>
<entry key="Width">15</entry>
</properties>

使用預設編碼將 Properties 條目儲存到 XML 檔案的示例

以下示例演示瞭如何使用 Java Properties storeToXML(OutputStream os, String comment, Charset encoding) 方法使用給定編碼將屬性物件儲存到 xml 檔案中,然後列印 xml。

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Properties;

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",Charset.defaultCharset());

         // print the xml.
         while (fis.available() > 0) {
            System.out.print("" + (char) fis.read());
         }
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

<?xml version="1.0" encoding="windows-1252"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Properties Example</comment>
<entry key="Height">200</entry>
<entry key="Width">15</entry>
</properties>
java_util_properties.htm
廣告
© . All rights reserved.