Java Properties store() 方法



描述

Java Properties store(OutputStream out,String comments) 方法將此屬性列表(鍵值對)以適合使用 load(InputStream) 方法載入到 Properties 表格的格式寫入此 Properties 表格中的輸出流。

宣告

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

public void store(OutputStream out,String comments)

引數

  • out − 輸出流。

  • comments − 屬性列表的描述。

返回值

此方法返回此屬性列表中指定鍵的前一個值,如果它沒有鍵,則返回 null。

異常

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

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

  • NullPointerException − 如果 out 為 null。

Java Properties store(Writer writer,String comments) 方法

描述

java Properties store(Writer writer,String comments) 方法將此屬性列表(鍵值對)以適合使用 load(Reader) 方法的格式寫入此 Properties 表格中的輸出字元流。

宣告

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

public void store(Writer writer,String comments)

引數

  • out − 輸出字元流寫入器。

  • comments − 屬性列表的描述。

返回值

此方法返回此屬性列表中指定鍵的前一個值,如果它沒有鍵,則返回 null。

異常

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

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

  • NullPointerException − 如果 out 為 null。

將屬性條目儲存到控制檯示例

以下示例顯示了 Java Properties store(OutputStream, String) 方法的使用方法。我們建立了一個 Properties 物件。然後添加了一些屬性並打印出來。然後我們將屬性儲存到 System.out 以在控制檯上列印。

package com.tutorialspoint;

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.setProperty("Height", "200");
      prop.put("Width", "1500");

      // print the list 
      System.out.println("" + prop);
      
      try {
         // store the properties list in an output stream
         prop.store(System.out, "PropertiesDemo");
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

輸出

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

{Height=200, Width=1500}
#PropertiesDemo
#Mon Dec 26 13:17:45 IST 2022
Height=200
Width=1500

將屬性條目儲存到字串示例

以下示例顯示了 Java Properties store(OutputStream, String) 方法的使用方法。我們建立了一個 Properties 物件。然後添加了一些屬性並打印出來。然後我們將屬性儲存到 System.out 以在控制檯上列印。

package com.tutorialspoint;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Properties;

public class PropertiesDemo {
   public static void main(String[] args) {
      Properties prop = new Properties();
      StringWriter sw = new StringWriter();

      // add some properties
      prop.setProperty("Height", "200");
      prop.put("Width", "1500");

      // print the list 
      System.out.println("" + prop);
      
      try {
      
         // store the properties list in an output writer
         prop.store(sw, "PropertiesDemo");

         // print the writer
         System.out.println("" + sw.toString());
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

輸出

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

{Height=200, Width=1500}
#PropertiesDemo
#Mon Dec 26 13:19:47 IST 2022
Height=200
Width=1500
java_util_properties.htm
廣告

© . All rights reserved.