国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

java獲取properties配置文件

2019-11-17 04:00:12
字體:
來源:轉載
供稿:網友
package test.bwl;    
   
import java.io.FileNotFoundException;    
import java.io.IOException;    
import java.io.InputStream;    
import java.util.PRoperties;    
   
public class Test {    
    private static Properties properties = new Properties();    
   
    public static void main(String[] args) {    
        try {    
            InputStream is = Test.class.getClassLoader().getResourceAsStream("cache.properties");    
            properties.load(is);    
            String size = properties.getProperty("cache.size");    
            writeLog("配置成功!" + size);    
        } catch (FileNotFoundException e) {    
            writeLog("配置文件不存在!" + e.getMessage());    
        } catch (IOException e) {    
            writeLog("讀取配置文件IO錯誤!" + e.getMessage());    
        }    
    }    
   
    public static void writeLog(String strLog) {    
        System.out.println(strLog);    
    }    
}   
package test.bwl;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Test {
private static Properties properties = new Properties();

public static void main(String[] args) {
try {
InputStream is = Test.class.getClassLoader().getResourceAsStream("cache.properties");
properties.load(is);
String size = properties.getProperty("cache.size");
writeLog("配置成功!" + size);
} catch (FileNotFoundException e) {
writeLog("配置文件不存在!" + e.getMessage());
} catch (IOException e) {
writeLog("讀取配置文件IO錯誤!" + e.getMessage());
}
}

public static void writeLog(String strLog) {
System.out.println(strLog);
}
}

  

package test.bwl;    
   
import java.io.File;    
import java.io.FileInputStream;    
import java.io.FileNotFoundException;    
import java.io.FilenameFilter;    
import java.io.IOException;    
import java.io.InputStream;    
import java.util.Collections;    
import java.util.HashMap;    
import java.util.Map;    
import java.util.Properties;    
import java.util.regex.Pattern;    
   
/**   
* 配置信息管理器   
*    
* @author      bwl    
* @version     1.0   
*/   
public class ConfigManager {    
   
    /**   
     * 提供單例對象的靜態內部類   
     */   
    private static class SingletonHolder {    
        public static ConfigManager instance = new ConfigManager();    
    }    
   
    /**   
     * 獲取對象實例   
     * @return   
     */   
    public static ConfigManager getInstance() {    
        return SingletonHolder.instance;    
    }    
   
    /**   
     * 存儲問題列表的Map   
     */   
    private Map<String, Properties> name2properties;    
   
    /**   
     * 構造方法,請使用getInstance()獲取實例   
     */   
    private ConfigManager() {    
        name2properties = Collections.synchronizedMap(new HashMap<String, Properties>());    
        doInit();    
    }    
   
    /**   
     * 初始化方法    
     */   
    private void doInit() {    
        try {    
            File path = new File("./conf/");    
            if (!path.exists()) {    
                System.out.println("ConfilgManager Init Error: There is no folder named 'conf' under src file.");    
                return;    
            }    
            File[] confFiles = path.listFiles(new DirFilter(".*//.properties"));////    
            for (int i = 0; i < confFiles.length; i++) {    
                File f = confFiles[i];    
                if (f.exists() && f.isFile()) {    
                    Properties properties = new Properties();    
                    InputStream is = new FileInputStream(f);    
                    properties.load(is);    
                    name2properties.put(f.getName(), properties);    
                }    
            }    
   
        } catch (FileNotFoundException e) {    
            e.printStackTrace();    
        } catch (IOException e) {    
            e.printStackTrace();    
        }    
    }    
   
    /**   
     * 獲取配置項的值   
     * @param fileName  配置文件的名稱   
     * @param key       關鍵碼的值   
     * @return  配置項   
     */   
    public String getProperty(String fileName, String key) {    
        if (fileName == null || fileName.length() == 0) {    
            return null;    
        }    
        Properties prop = name2properties.get(fileName);    
        if (prop != null) {    
            return prop.getProperty(key);    
        }    
        return null;    
    }    
   
    /**   
     * 獲取整形的配置項的值   
     * @param fileName  配置文件的名稱   
     * @param keyName   關鍵碼的值   
     * @return  如果正確則返回數字,否則返回-1   
     */   
    public int getIntProperty(String fileName, String key) {    
        String value = this.getProperty(fileName, key);    
        int result = -1;    
        if (value == null) {    
            return result;    
        }    
        try {    
            result = Integer.parseInt(value);    
            return result;    
        } catch (Exception e) {    
            //Do nothing    
        }    
        return result;    
    }    
   
    /**   
     * 過濾屬性文件的內部類    
     */   
    class DirFilter implements FilenameFilter {    
   
        /**   
         * 記錄文件名格式的正則對象   
         */   
        private Pattern pattern;    
   
        public DirFilter(String regex) {    
            pattern = Pattern.compile(regex);    
        }    
   
        public boolean accept(File dir, String name) {    
            return pattern.matcher(new File(name).getName()).matches();    
        }    
   
    }    
   
    public static void main(String[] args) {    
        ConfigManager config = ConfigManager.getInstance();    
        System.out.println(config.getIntProperty("cache.properties", "cache.size") + "");    
        System.out.println(config.getProperty("javagroups.properties", "bus_name") + "");    
    }    
   
}   
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 六枝特区| 江山市| 太康县| 沙湾县| 彰武县| 临清市| 绥德县| 德惠市| 鹤峰县| 彭山县| 瑞安市| 龙江县| 玉田县| 定南县| 托克托县| 汉沽区| 元阳县| 柘荣县| 关岭| 桃江县| 武隆县| 霞浦县| 桂平市| 中卫市| 阳曲县| 台湾省| 云安县| 安康市| 丹寨县| 独山县| 林州市| 景德镇市| 济南市| 那曲县| 天等县| 兴海县| 桃园县| 布拖县| 龙里县| 长沙县| 无棣县|