Java PropertyResourceBundle 的 handleGetObject(String) 方法



描述

Java PropertyResourceBundle 的 handleGetObject(String) 方法返回捆綁包中指定鍵對應的物件。如果捆綁包不包含所需的物件,則該方法返回null

宣告

以下是java.util.PropertyResourceBundle.handleGetObject(String) 方法的宣告

public Object handleGetObject(String key)

引數

key - 所需物件的鍵。

返回值

此方法返回指定鍵對應的物件。

異常

從位元組流中的 PropertyResourceBundle 例項獲取物件示例

以下示例演示了 Java PropertyResourceBundle handleGetObject() 方法的使用。我們建立了一個給定內容的 InputStream,然後使用該流建立了一個 PropertyResourceBundle 物件。然後使用 handleGetObject() 方法獲取所有資源,並列印其值。

package com.tutorialspoint;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import java.util.PropertyResourceBundle;

public class PropertyResourceBundleDemo {
   public static void main(String[] args) {

      // Prepare content for simulating property files
      String fileContent = 
         "# Integer value 1\n" +
         "s1 = 1\n" +
         "# String value PropertyResourceBundleDemo\n" +
         "s2 = PropertyResourceBundleDemo\n" +
         "# Date value Fri Jan 31 00:00:00 IST 3913\n" +
         "s3 = Fri Jan 31 00:00:00 IST 3913";
      InputStream propStream = new StringBufferInputStream(fileContent);
         
      try {

         // Create property resource bundle
         PropertyResourceBundle bundle = 
         new PropertyResourceBundle(propStream);

         // Get resources
         Object res1 = bundle.handleGetObject("s1");
         Object res2 = bundle.handleGetObject("s2");
         Object res3 = bundle.handleGetObject("s3");

         // Print resource contents
         System.out.println("[Resource1] "+res1);
         System.out.println("[Resource2] "+res2);
         System.out.println("[Resource3] "+res3);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

輸出

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

[Resource1] 1
[Resource2] PropertyResourceBundleDemo
[Resource3] Fri Jan 31 00:00:00 IST 3913

從 StringReader 中的 PropertyResourceBundle 例項獲取物件示例

以下示例演示了 Java PropertyResourceBundle handleGetObject() 方法的使用。我們建立了一個給定內容的 InputStream,然後使用該流建立了一個 PropertyResourceBundle 物件。然後使用 handleGetObject() 方法獲取所有資源,並列印其值。

package com.tutorialspoint;

import java.io.IOException;
import java.io.StringReader;
import java.util.PropertyResourceBundle;

public class PropertyResourceBundleDemo {
   public static void main(String[] args) {

      // Prepare content for simulating property files
      String fileContent = 
         "# Integer value 1\n" +
         "s1 = 1\n" +
         "# String value PropertyResourceBundleDemo\n" +
         "s2 = PropertyResourceBundleDemo\n" +
         "# Date value Fri Jan 31 00:00:00 IST 3913\n" +
         "s3 = Fri Jan 31 00:00:00 IST 3913";
      StringReader reader = new StringReader(fileContent);

      try {
      
         // Create property resource bundle
         PropertyResourceBundle bundle = 
         new PropertyResourceBundle(reader);

         // Get resources
         Object res1 = bundle.handleGetObject("s1");
         Object res2 = bundle.handleGetObject("s2");
         Object res3 = bundle.handleGetObject("s3");

         // Print resource contents
         System.out.println("[Resource1] "+res1);
         System.out.println("[Resource2] "+res2);
         System.out.println("[Resource3] "+res3);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

輸出

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

[Resource1] 1
[Resource2] PropertyResourceBundleDemo
[Resource3] Fri Jan 31 00:00:00 IST 3913

java_util_propertyresourcebundle.htm
廣告

© . All rights reserved.