JAVA訪問設定檔總結

來源:互聯網
上載者:User

標籤:

一、全域配置的簡單 propertie 檔案實現

 1 package com.testgs.utils; 2  3 import java.util.*; 4 import java.io.*; 5  6  7 public final class ARConfig { 8  9     private Properties conf = new Properties();10     private String prefix = "";11     /**12      * 全域設定檔名13      */14     public static final String GLOBAL_CONF_FILE = "/analysisReportConfig.properties";15 16     public ARConfig(String prefix) {17         this.prefix = prefix;18         loadConf();19     }20 21     /**22      * 取得屬性檔案執行個體23      * @param prefix 各資料庫連接首碼24      * @return25      */26     public synchronized static ARConfig getInstance(String prefix) {27         return new ARConfig(prefix);28     }29     30     public String getConfString(String name, String defaultValue) {31         String result = getConfString(name);32         result = (result == null) ? defaultValue : result;33         return result;34     }35 36     /**讀取配置資訊的 boolean 值37      * @param name38      * @param defaultValue39      * @return40      */41     public boolean getConfBoolean(String name, boolean defaultValue) {42         boolean result = defaultValue;43         String value = getConfString(name);44         if (value != null) {45             value = value.toLowerCase();46             result = value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes");47         }48         return result;49     }50 51     /**讀取配置資訊的 boolean 值,如果沒有,預設為 false52      * @param name53      * @return54      */55     public boolean getConfBoolean(String name) {56         return getConfBoolean(name, false);57     }58 59     /**60      * 讀取配置資訊的 int 值61      * @param name62      * @param defaultValue63      * @return64      */65     public int getConfigInt(String name, int defaultValue) {66         String intV = getConfString(name);67         int result = defaultValue;68         if (intV != null) {69             try {70                 result = Integer.parseInt(intV.trim());71             } catch (Exception e) {72                 e.printStackTrace();73             }74         }75         return result;76     }77 78     public String getConfString(String name) {79         name = this.prefix + name;80         return conf.getProperty(name);81     }82 83     protected synchronized void loadConf() {84         conf.clear();85         InputStream input = null;86         try {87             input = this.getClass().getResourceAsStream(GLOBAL_CONF_FILE);88             conf.load(input);89         } catch (IOException e) {90             throw new RuntimeException("找不到設定檔: " + GLOBAL_CONF_FILE);91         } finally {92             if (input != null)93                 try {94                     input.close();95                 } catch (Exception closeE) {96                 }97         }98     }99 }
訪問 properties 檔案

 更新中。。。

JAVA訪問設定檔總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.