Java Property類使用詳解
概念理解
Properties 繼承于 Hashtable。表示一個持久的屬性集,屬性列表以key-value的形式存在,key和value都是字符串。Properties類被許多Java類使用。例如,在獲取環(huán)境遍歷時它就作為System.getProperties()方法的返回值。我們在很多需要避免硬編碼的應(yīng)用場景下需要使用Properties文件來加載程序需要配置的信息,比如JDBC、MyBatis框架等。Properties類則是Properties文件和程序的中間橋梁,不論是從properties文件讀取信息還是寫入信息到properties文件都要經(jīng)由Properties類。
寫入
Properties類調(diào)用setProperty方法將鍵值對保存到內(nèi)存中,此時可以通過getProperty方法讀取,propertyNames方法進行遍歷,但是并沒有將鍵值對持久化到屬性文件中,故需要調(diào)用store方法持久化鍵值對到屬性文件中。
我們寫一個類測試
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Date; import java.util.Properties; public class TestProperties { public void writeProperties() { Properties properties = new Properties(); OutputStream output = null; try { output = new FileOutputStream("config.properties"); properties.setProperty("url", "jdbc:mysql://localhost:3306/"); properties.setProperty("username", "root"); properties.setProperty("password", "root"); properties.setProperty("databases", "music_player"); properties.store(output, "Steven1997 modify" + new Date().toString()); } catch (IOException e) { e.printStackTrace(); }finally { if(output!=null) { try { output.close(); }catch (IOException e) { e.printStackTrace(); } } } } public static void main(String[] args) { TestProperties t = new TestProperties(); t.writeProperties(); } }
執(zhí)行后,工程下面會出現(xiàn)一個config.properties文件,屬性文件內(nèi)容如下:
讀取
使用getProperty獲取config.properties文件配置文件的各項屬性。
package property; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class LoadProperties { public void loadProperties() { Properties properties = new Properties(); InputStream inputStream = null; try { inputStream = new FileInputStream("config.properties"); properties.load(inputStream); System.out.println("url:" + properties.getProperty("url")); System.out.println("username:" + properties.getProperty("username")); System.out.println("password:" + properties.getProperty("password")); System.out.println("database:" + properties.getProperty("database")); } catch (IOException e) { e.printStackTrace(); }finally { if(inputStream !=null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String[] args) { LoadProperties l = new LoadProperties(); l.loadProperties(); } }
運行后的結(jié)果
url:jdbc:mysql://localhost:3306/
username:root
password:root
database:music_player
遍歷
遍歷屬性文件中的鍵值對
package property; import java.io.InputStream; import java.util.Enumeration; import java.util.Map.Entry; import java.util.Properties; import java.util.Set; public class PropertiesTest { public void printAll() { Properties prop = new Properties(); InputStream input = null; try { String file = "config.properties"; input = getClass().getClassLoader().getResourceAsStream(file); if(input == null) { System.out.println("無法加載文件" + file); return ; } prop.load(input); // 方法一 Set<Object> keys = prop.keySet(); for(Object key:keys) { System.out.println("key:" + key.toString() + "|" + "value:" + prop.get(key)); } //方法二: Set<Entry<Object, Object>> entrys = prop.entrySet();//返回的屬性鍵值對實體 for(Entry<Object, Object> entry:entrys){ System.out.println("key:"+entry.getKey()+",value:"+entry.getValue()); } //方法三: Enumeration<?> e = prop.propertyNames(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); String value = prop.getProperty(key); System.out.println("Key:" + key + ",Value:" + value); } }catch (Exception e) { e.printStackTrace(); }finally { if(input != null) { try { input.close(); }catch (Exception e) { e.printStackTrace(); } } } } public static void main(String[] args) { PropertiesTest p = new PropertiesTest(); p.printAll(); } }
運行結(jié)果如下:
key:url|value:jdbc:mysql://localhost:3306/
key:password|value:root
key:database|value:music_player
key:username|value:root
key:url,value:jdbc:mysql://localhost:3306/
key:password,value:root
key:database,value:music_player
key:username,value:root
Key:password,Value:root
Key:url,Value:jdbc:mysql://localhost:3306/
Key:database,Value:music_player
Key:username,Value:root
properties文件默認的編碼格式居然是ISO-8859-1,這樣導(dǎo)致往配置文件里面寫入中文的時候轉(zhuǎn)換成另一種格式的編碼,需要把properties 文件的編碼格式改為UTF-8,這樣才會讓配置文件保存中文數(shù)據(jù)的時候不會出現(xiàn)轉(zhuǎn)碼的問題
以上所述是小編給大家介紹的Java Property類使用詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- java property配置文件管理工具框架過程詳解
- Mybatis單個參數(shù)的if判斷報異常There is no getter for property named ''xxx'' in ''class java.lang.Integer''的解決方案
- Java使用application.property讀取文件里面的值
- 詳解java中的PropertyChangeSupport與PropertyChangeListener
- Java通過PropertyDescriptor反射調(diào)用set和get方法
- Java的System.getProperty()方法獲取大全
- JSP JavaBean的setProperty屬性
- Java加載property文件配置過程解析
相關(guān)文章
mybatis的association傳遞參數(shù)問題示例
這篇文章主要介紹了mybatis的association傳遞參數(shù)問題,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12Java實現(xiàn)升級版布谷鳥闖關(guān)游戲的示例代碼
升級版布谷鳥闖關(guān)游戲是一個基于java的布谷鳥闖關(guān)游戲,鼠標左鍵點擊控制鳥的位置穿過管道間的縫隙。文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2022-02-02Java使用Collections工具類對List集合進行排序
這篇文章主要介紹了Java使用Collections工具類對List集合進行排序,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10SpringMVC如何在生產(chǎn)環(huán)境禁用Swagger的方法
本篇文章主要介紹了SpringMVC如何在生產(chǎn)環(huán)境禁用Swagger的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02HttpClient的DnsResolver自定義DNS解析另一種選擇深入研究
這篇文章主要為大家介紹了HttpClient的DnsResolver自定義DNS解析另一種選擇深入研究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10java 矩陣乘法的mapreduce程序?qū)崿F(xiàn)
這篇文章主要介紹了java 矩陣乘法的mapreduce程序?qū)崿F(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-06-06idea hibernate jpa 生成實體類的實現(xiàn)
這篇文章主要介紹了idea hibernate jpa 生成實體類的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11