讀取properties文件

記錄工作中常用方法,方便以後使用


InputStream inputStream = MyClass.class.getClassLoader().getResourceAsStream("config.properties"); //還有另一種方法
Properties p = new Properties();
try {
    p.load(inputStream);
} catch (IOException e) {
    logger.error("配置文件config.properties載入出錯", e);
    System.exit(-1);  //文件不存在
}
		
if((config = p.getProperty(Config_Name)) == null){
    logger.error("配置文件中 {} 字段不存在", Config_Name);
    System.exit(-1);
}

以下2個方法效果一樣
PropertisUtil.class.getResourceAsStream("/config.properties");
PropertisUtil.class.getClassLoader().getResourceAsStream("config.properties");


以他方法參照

http://blog.csdn.net/senton/article/details/4083127

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章