java讀取resource下指定xxx.properties配置檔案內容
阿新 • • 發佈:2019-02-06
1.在做以公司的專案開發 ,發現該專案開發了一年了 ,還沒有讀取配置檔案的工具類(可能是我沒找到) ,於是就參考之前的專案 ,寫了一個自己用著๑乛◡乛๑
2.如果你拿來用 ,會發現少jar包 ,關於匯入什麼jar包 ,自己研究去 ,度娘谷哥都知道 ,我就不告訴你了o(=·ω·=)m
廢話說完了,程式碼如下:
/** * PropertiesUtil. * Created by yf on 2017/5/26. */ public final class PropertiesUtil { //日誌,不必要 private static Logger _loger = LoggerFactory.getLogger(PropertiesUtil.class); //指定配置檔案地址//resource目錄下: private static PropertiesLoader propertiesLoader = new PropertiesLoader( "classpath:/appconfig.properties"); public static String getProperty(String key) { String property; try { property = propertiesLoader.getProperty(key); } catch (Exception e) { _loger.debug("Property key not exit: " + key); return null; } return property; } public static String getProperty(String key, String value) { if (propertiesLoader.getProperty(key) == null) { return value; } return propertiesLoader.getProperty(key); } }
讀取配置檔案的方法有很多 ,這只是其中一個 ,如果不適用 , 還能搜尋到很多方案
加上現在的java專案基本都是使用了spring全家桶 , 讀取配置資訊更加容易了