Java 操作Properties配置文件詳解
1 簡(jiǎn)介:
JDK提供的java.util.Properties類繼承自Hashtable類并且實(shí)現(xiàn)了Map接口,是使用一種鍵值對(duì)的形式來(lái)保存屬性集,其中鍵和值都是字符串類型。
java.util.Properties類提供了getProperty()和setProperty()方法來(lái)操作屬性文件,同時(shí)使用load()方法和store()方法加載和保存Properties配置文件。
java.util.ResourceBundle類也提供了讀取Properties配置文件的方法,ResourceBundle是一個(gè)抽象類。
2.Properties中的主要方法
1)load(InputStream inStream):該方法可以從.properties屬性文件對(duì)應(yīng)的文件數(shù)入流中,加載屬性列表到Properties類對(duì)象中。load有兩個(gè)方法的重載:load(InputStream inStream)、load(Reader reader),可根據(jù)不同的方式來(lái)加載屬性文件。
InputStream inStream = TestProperties.class.getClassLoader().getResourceAsStream("demo.properties"); //通過(guò)當(dāng)前類加載器的getResourceAsStream方法獲取 //TestProperties當(dāng)前類名;TestProperties.class.取得當(dāng)前對(duì)象所屬的Class對(duì)象; getClassLoader():取得該Class對(duì)象的類裝載器 InputStream in = ClassLoader.getSystemResourceAsStream("filePath"); InputStream inStream = new FileInputStream(new File("filePath")); //從文件獲取 InputStream in = context.getResourceAsStream("filePath"); //在servlet中,可以通過(guò)context來(lái)獲取InputStream InputStream inStream = new URL("path").openStream(); //通過(guò)URL來(lái)獲取
讀取方法如下:
Properties pro = new Properties(); //實(shí)例化一個(gè)Properties對(duì)象 InputStream inStream = new FileInputStream("demo.properties"); //獲取屬性文件的文件輸入流 pro.load(nStream); inStream.close();
2)store(OutputStream out,String comments):這個(gè)方法將Properties類對(duì)象的屬性列表寫(xiě)入.properties配置文件。如下:
FileOutputStream outStream = new FileOutputStream("demo.properties"); pro.store(outStream,"Comment"); outStream.close();
3 ResourceBundle中的主要方法
通過(guò)ResourceBundle.getBundle()靜態(tài)方法來(lái)獲取,此方法獲取properties屬性文件不需要加.properties后綴名。也可以從InputStream中獲取ResourceBundle對(duì)象。
ResourceBundle resource = ResourceBundle.getBundle("com/xiang/demo");//emo為屬性文件名,放在包c(diǎn)om.xiang下,如果是放在src下,直接用test即可 ResourceBundle resource1 = new PropertyResourceBundle(inStream); String value = resource.getString("name");
在使用中遇到的問(wèn)題可能是配置文件的路徑,當(dāng)配置文件不在當(dāng)前類所在的包下,則需要使用包名限定;若屬性文件在src根目錄下,則直接使用demo.properties或demo即可。
4 Properties操作實(shí)例
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Properties; /** * Java中Preperties配置文件工具類 * @author shu * */ public class PropsUtil { private String path = ""; private Properties properties ; /** * 默認(rèn)構(gòu)造函數(shù) */ public PropsUtil() {} /** * 構(gòu)造函數(shù) * @param path 傳入Properties地址值 */ public PropsUtil(String path) { this.path = path; } /** * 加載properties文件 * @return 返回讀取到的properties對(duì)象 */ public Properties loadProps(){ InputStream inStream = ClassLoader.getSystemResourceAsStream(path); try { if(inStream==null) throw new FileNotFoundException(path + " file is not found"); properties = new Properties(); properties.load(inStream); inStream.close(); } catch (IOException e) { e.printStackTrace(); } return properties; } /** * 將配置寫(xiě)入到文件 */ public void writeFile(){ // 獲取文件輸出流 try { FileOutputStream outputStream = new FileOutputStream( new File(ClassLoader.getSystemResource(path).toURI())); properties.store(outputStream, null); outputStream.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 通過(guò)關(guān)鍵字獲取值 * @param key * @return 返回對(duì)應(yīng)的字符串,如果無(wú),返回null */ public String getValueByKey(String key) { if(properties==null) properties = loadProps(); String val = properties.getProperty(key.trim()); return val; } /** * 通過(guò)關(guān)鍵字獲取值 * @param key 需要獲取的關(guān)鍵字 * @param defaultValue 若找不到對(duì)應(yīng)的關(guān)鍵字時(shí)返回的值 * @return 返回找到的字符串 */ public String getValueByKey(String key,String defaultValue){ if(properties==null) properties = loadProps(); return properties.getProperty(key, defaultValue); } /** * 獲取Properties所有的值 * @return 返回Properties的鍵值對(duì) */ public Map<String, String> getAllProperties() { if(properties==null) properties = loadProps(); Map<String, String> map = new HashMap<String, String>(); // 獲取所有的鍵值 Iterator<String> it=properties.stringPropertyNames().iterator(); while(it.hasNext()){ String key=it.next(); map.put(key, properties.getProperty(key)); } /*Enumeration enumeration = properties.propertyNames(); while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement(); String value = getValueByKey(key); map.put(key, value); }*/ return map; } /** * 往Properties寫(xiě)入新的鍵值且保存 * @param key 對(duì)應(yīng)的鍵 * @param value 對(duì)應(yīng)的值 */ public void addProperties(String key, String value) { if(properties==null) properties = loadProps(); properties.setProperty(key, value); try { writeFile(); } catch (Exception e) { throw new RuntimeException("write fail"); } } /** * 更新配置文件 * @param key 對(duì)應(yīng)的鍵 * @param value 對(duì)應(yīng)的值 */ public void update(String key,String value){ if(properties==null) properties = loadProps(); if(properties.containsKey(key)) properties.replace(key, value); try { writeFile(); } catch (Exception e) { throw new RuntimeException("write fail"); } } /** * 刪除某一鍵值對(duì) * @param key */ public void deleteByKey(String key){ if(properties==null) properties = loadProps(); if(!properties.containsKey(key)) throw new RuntimeException("not such key"); properties.remove(key); try { writeFile(); } catch (Exception e) { throw new RuntimeException("write fail"); } } /** * 設(shè)置path值 * @param path */ public void setPath(String path){ this.path = path; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談SpringBoot項(xiàng)目如何讓前端開(kāi)發(fā)提高效率(小技巧)
這篇文章主要介紹了淺談SpringBoot項(xiàng)目如何讓前端開(kāi)發(fā)提高效率(小技巧),主要介紹了Swagger和Nginx提高效率的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04Spring Boot + Thymeleaf + Activiti 快速開(kāi)發(fā)平臺(tái)項(xiàng)目 附源碼
這篇文章主要介紹了Spring Boot + Thymeleaf + Activiti 快速開(kāi)發(fā)平臺(tái)項(xiàng)目附源碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04解決springMVC 跳轉(zhuǎn)js css圖片等靜態(tài)資源無(wú)法加載的問(wèn)題
下面小編就為大家?guī)?lái)一篇解決springMVC 跳轉(zhuǎn)js css圖片等靜態(tài)資源無(wú)法加載的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10IDEA JavaWeb項(xiàng)目啟動(dòng)運(yùn)行后出現(xiàn)404錯(cuò)誤的解決方法
這篇文章主要介紹了IDEA JavaWeb項(xiàng)目啟動(dòng)運(yùn)行后出現(xiàn)404錯(cuò)誤的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12SpringBoot+Quartz實(shí)現(xiàn)定時(shí)任務(wù)的代碼模版分享
quartz?是一款開(kāi)源且豐富特性的Java?任務(wù)調(diào)度庫(kù),用于實(shí)現(xiàn)任務(wù)調(diào)度和定時(shí)任務(wù),本文主要和大家分享一個(gè)SpringBoot整合Quartz實(shí)現(xiàn)定時(shí)任務(wù)的代碼模版,需要的可以參考一下2023-06-06SpringBoot 任務(wù)調(diào)度動(dòng)態(tài)設(shè)置方式(不用重啟服務(wù))
這篇文章主要介紹了SpringBoot 任務(wù)調(diào)度 動(dòng)態(tài)設(shè)置方式(不用重啟服務(wù)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11MyBatis-Plus中MetaObjectHandler沒(méi)生效完美解決
在進(jìn)行測(cè)試時(shí)發(fā)現(xiàn)配置的MyMetaObjectHandler并沒(méi)有生效,本文主要介紹了MyBatis-Plus中MetaObjectHandler沒(méi)生效完美解決,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11深度剖析java中JDK動(dòng)態(tài)代理機(jī)制
本篇文章主要介紹了深度剖析java中JDK動(dòng)態(tài)代理機(jī)制 ,動(dòng)態(tài)代理避免了開(kāi)發(fā)人員編寫(xiě)各個(gè)繁鎖的靜態(tài)代理類,只需簡(jiǎn)單地指定一組接口及目標(biāo)類對(duì)象就能動(dòng)態(tài)的獲得代理對(duì)象。2017-04-04