Java Properties loadFromXML() 方法



描述

java Properties loadFromXML(InputStream in) 方法將指定輸入流上的 XML 文件中表示的所有屬性載入到此屬性表中。

宣告

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

public void loadFromXML(InputStream in)

引數

in − 用於讀取 XML 文件的輸入流。

返回值

此方法不返回值。

異常

  • IOException − 如果從指定的輸入流讀取導致 IOException。

  • InvalidPropertiesFormatException − 輸入流上的資料不構成具有強制性文件型別的有效 XML 文件。

  • NullPointerException − 如果 in 為 null。

從 XML 檔案載入屬性示例

以下示例演示瞭如何使用 Java Properties loadFromXML(Stream) 方法從 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, null);

         // load from the xml that we saved earlier
         prop.loadFromXML(fis);

         // print the properties list
         prop.list(System.out);
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

輸出

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

-- listing properties --
Height=200
Width=15
java_util_properties.htm
廣告