java讀取.properties配置檔案的幾種方法
讀取.properties配置檔案在實際的開發中使用的很多,總結了一下,有以下幾種方法(僅僅是我知道的):
一.通過jdk提供的java.util.Properties類
此類繼承自java.util.HashTable,即實現了Map介面,所以,可使用相應的方法來操作屬性檔案,但不建議使用像put、putAll這 兩個方法,因為put方法不僅允許存入String型別的value,還可以存入Object型別的。因此java.util.Properties類提 供了getProperty()和setProperty()方法來操作屬性檔案,同時使用store或save(已過時)來儲存屬性值(把屬性值寫 入.properties配置檔案)。在使用之前,還需要載入屬性檔案,它提供了兩個方法:load和loadFromXML。
load有兩個方法的過載:load(InputStream inStream)、load(Reader reader),所以,可根據不同的方式來載入屬性檔案。
可根據不同的方式來獲取InputStream,如:
1.通過當前類載入器的getResourceAsStream方法獲取下載地址
- InputStream inStream = TestProperties.class.getClassLoader().getResourceAsStream("test.properties");
2.從檔案獲取
-
InputStream inStream =
3.也是通過類載入器來獲取,和第一種一樣
- InputStream in = ClassLoader.getSystemResourceAsStream("filePath");
4.在servlet中,還可以通過context來獲取InputStream
- InputStream in = context.getResourceAsStream("filePath");
5.通過URL來獲取
- URL url = new URL("path");
-
InputStream inStream = url.openStream();
讀取方法如下:
- Properties prop = new Properties();
- prop.load(inStream);
- String key = prop.getProperty("username");
- //String key = (String) prop.get("username");
二.通過java.util.ResourceBundle類讀取
這種方式比使用Properties要方便一些。
1.通過ResourceBundle.getBundle()靜態方法來獲取
ResourceBundle是一個抽象類,這種方式來獲取properties屬性檔案不需要加.properties字尾名,只需要檔名即可。
- ResourceBundle resource = ResourceBundle.getBundle("com/mmq/test");//test為屬性檔名,放在包com.mmq下,如果是放在src下,直接用test即可
- String key = resource.getString("username");
2.從InputStream中讀取
獲取InputStream的方法和上面一樣,不再贅述。
- ResourceBundle resource = new PropertyResourceBundle(inStream);
注意:在使用中遇到的最大的問題可能是配置檔案的路徑問題,如果配置檔案入在當前類所在的包下,那麼需要使用包名限定, 如:test.properties入在com.mmq包下,則要使用com/mmq/test.properties(通過Properties來獲 取)或com/mmq/test(通過ResourceBundle來獲取);屬性檔案在src根目錄下,則直接使用test.properties 下載地址 或test即可。
三.例項
ResourceLoader.java
- package com.bijian.study;
- import java.io.File;
- import java.io.FileInputStream;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Properties;
- publicfinalclass ResourceLoader {
- privatestatic ResourceLoader loader = new ResourceLoader();
- privatestatic Map<String, Properties> loaderMap = new HashMap<String, Properties>();
- private ResourceLoader() {
- }
- publicstatic ResourceLoader getInstance() {
- return loader;
- }
- public Properties getPropFromProperties(String fileName) throws Exception {
- Properties prop = loaderMap.get(fileName);
- if (prop != null) {
- return prop;
- }
- String filePath = null;
- String configPath = System.getProperty("configurePath");
- if (configPath == null) {
- filePath = this.getClass().getClassLoader().getResource(fileName).getPath();
- } else {
- filePath = configPath + "/" + fileName;
- }
- prop = new Properties();
- prop.load(new FileInputStream(new File(filePath)));
- loaderMap.put(fileName, prop);
- return prop;
- }
- }
PropertiesUtil.java
- package com.bijian.study;
- import java.util.Properties;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.concurrent.ConcurrentMap;
- /**
- * 用ConcurrentMap來快取屬性檔案的key-value
- */
- publicclass PropertiesUtil {
- privatestatic ResourceLoader loader = ResourceLoader.getInstance();
- privatestatic ConcurrentMap<String, String> configMap = new ConcurrentHashMap<String, String>();
- privatestaticfinal String DEFAULT_CONFIG_FILE = "test.properties";
- privatestatic Properties prop = null;
- publicstatic String getStringByKey(String key, String propName) {
- try {
- prop = loader.getPropFromProperties(propName);
- } catch (Exception e) {
- thrownew RuntimeException(e);
- }
- key = key.trim();
- if (!configMap.containsKey(key)) {
- if (prop.getProperty(key) != null) {
- configMap.put(key, prop.getProperty(key));
- }
- }
- return configMap.get(key);
- }
- publicstatic String getStringByKey(String key) {
- return getStringByKey(key, DEFAULT_CONFIG_FILE);
- }
- publicstatic Properties getProperties() {
- try {
- return loader.getPropFromProperties(DEFAULT_CONFIG_FILE);
- } catch (Exception e) {
- e.printStackTrace();
- returnnull;
- }
- }
- }
Constant.java
- package com.bijian.study;
- publicclass Constant {
- publicstaticfinal String TEST = PropertiesUtil.getStringByKey("test", "test.properties");
- }
Main.java
- package com.bijian.study;
- publicclass Main {
- publicstaticvoid main(String[] args) {
- System.out.println(Constant.TEST);
- }
- }