首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> J2SE

使用 J2SE API 讀取 Properties 檔案的六種方法

     編輯:J2SE

六種讀取方式

  1. Properties.load(InputStream):最基礎,配合 FileInputStream/BufferedInputStream。
  2. ResourceBundle.getBundle(String, Locale):國際化資源包機制,自動依 Locale 尋找 _zh_TW.properties 等變體。
  3. new PropertyResourceBundle(InputStream):將輸入流包裝為 ResourceBundle,再轉 Properties。
  4. Class.getResourceAsStream(String):相對類路徑載入 (同 package 下檔案)。
  5. Class.getClassLoader().getResourceAsStream(String):類載入器路徑 (classpath 根目錄)。
  6. ClassLoader.getSystemResourceAsStream(String):系統類載入器 (應用程式 classpath 根)。

補充:Servlet 環境


InputStream in = servletContext.getResourceAsStream("/WEB-INF/config.properties");
Properties p = new Properties(); p.load(in);

完整範例:JProperties.java


package com.kindani;
import java.util.*; import java.io.*;

public class JProperties {
    public static final int BY_PROPERTIES = 1;
    public static final int BY_RESOURCEBUNDLE = 2;
    public static final int BY_PROPERTYRESOURCEBUNDLE = 3;
    public static final int BY_CLASS = 4;
    public static final int BY_CLASSLOADER = 5;
    public static final int BY_SYSTEM_CLASSLOADER = 6;

    public static Properties loadProperties(String name, int type) throws IOException {
        Properties p = new Properties(); InputStream in = null;
        try {
            switch (type) {
                case BY_PROPERTIES:
                    in = new BufferedInputStream(new FileInputStream(name)); p.load(in); break;
                case BY_RESOURCEBUNDLE:
                    ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
                    p = new ResourceBundleAdapter(rb); break;
                case BY_PROPERTYRESOURCEBUNDLE:
                    in = new BufferedInputStream(new FileInputStream(name));
                    p = new ResourceBundleAdapter(new PropertyResourceBundle(in)); break;
                case BY_CLASS:
                    in = JProperties.class.getResourceAsStream(name); p.load(in); break;
                case BY_CLASSLOADER:
                    in = JProperties.class.getClassLoader().getResourceAsStream(name); p.load(in); break;
                case BY_SYSTEM_CLASSLOADER:
                    in = ClassLoader.getSystemResourceAsStream(name); p.load(in); break;
            }
        } finally { if (in != null) in.close(); }
        return p;
    }

    // ResourceBundle -> Properties 適配器
    static class ResourceBundleAdapter extends Properties {
        ResourceBundleAdapter(ResourceBundle rb) {
            for (Enumeration e = rb.getKeys(); e.hasMoreElements(); ) {
                String k = e.nextElement(); put(k, rb.getObject(k));
            }
        }
    }
}

選擇建議

  • 獨立應用、固定路徑 → FileInputStream (方式 1)。
  • 需國際化、多語系 → ResourceBundle (方式 2/3)。
  • 資源檔打包在 JAR 內、隨類載入 → Class.getResourceAsStream (方式 4)。
  • 資源檔在 classpath 根目錄 (如 src/main/resources) → 方式 5 或 6。

本文來自編程入門網:原文連結

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved