如何獲取.properties文件及讀取配置

引子:如圖所示,此配置文件是如何讀取到的呢?其裏面配的值,又是如何得到的呢?

1、怎麼讀取配置文件(比如 resources下的smart.properties)?

InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); //此處fileName是smart.properties
Properties properties = new Properties(); //java.util.Properties
properties.load(properties);

2、怎麼獲取配置文件(smart.properties)裏的值(如 smart.framework.jdbc.driver=com.mysql.jdbc.Driver)?

String key = "smart.framework.jdbc.driver";
if(properties.containsKey(key)){
    properties.getProperties(key);
}

 

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