[Java基础] Properties类的基本操作和介绍
引言
Java中的.properties文件是一種配置文件,主要用于表達(dá)配置信息;通俗來(lái)說(shuō),存放的數(shù)據(jù)就像是Map中的key和value的對(duì)應(yīng)關(guān)系一樣;這樣就可以通過(guò)鍵值對(duì)來(lái)對(duì)屬性進(jìn)行匹配,并且屬性列表中每個(gè)鍵及其對(duì)應(yīng)值都是一個(gè)字符串(String類型);
在Java中存在一個(gè)Properties類可以用來(lái)保存屬性集,它可以從流中加載屬性集;所以接下來(lái)我將會(huì)詳細(xì)介紹一下如何使用Properties類以及其中的常用的方法;
Properties類和和常用方法基本介紹
Properties:
中文:
Properties 類表示了一個(gè)持久的屬性集。Properties 可保存在流中或從流中加載。屬性列表中每個(gè)鍵及其對(duì)應(yīng)值都是一個(gè)字符串。
一個(gè)屬性列表可包含另一個(gè)屬性列表作為它的“默認(rèn)值”;如果未能在原有的屬性列表中搜索到屬性鍵,則搜索第二個(gè)屬性列表。
英文:
The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.
A property list can contain another property list as its “defaults”; this second property list is searched if the property key is not found in the original property list.
這里簡(jiǎn)單列舉幾個(gè)常用的方法,接下來(lái)的操作我也將會(huì)用到這些方法;
1,String getProperty(String key)
簡(jiǎn)介:通過(guò)key獲取value
2,void load(InputStream inStream/Reader reader)
簡(jiǎn)介:將文件內(nèi)容讀入內(nèi)存
3,Object setProperty(String key, String value)
簡(jiǎn)介:設(shè)置(增加)新的鍵值對(duì)
4,void store(OutputStream out / Writer writer, String comments)
簡(jiǎn)介:將內(nèi)存中對(duì)應(yīng)內(nèi)容寫入文件
這幾個(gè)方法是比較常用的,當(dāng)然還有幾個(gè)方法沒(méi)有寫,如果需要可以在API文檔中查詢;
接下來(lái)我將會(huì)用代碼實(shí)現(xiàn)一些操作,在這里先把.properties文件放出來(lái):
下面的代碼操作都是基于這個(gè)文件;
讀取操作
讀取操作可以通過(guò)load方法讀取文件,再通過(guò)getProperty獲取value值;
下面我用一個(gè)代碼示范:
輸出結(jié)果:
jack 18 man寫入操作
寫入操作就有點(diǎn)意思了,這里寫入分為三種情況:
這三種情況一般按需求使用,但是我感覺(jué)最常用的還是第三種(只是個(gè)人觀點(diǎn));
下面我用代碼示范一下,代碼中已有詳細(xì)注釋;
如果不太理解就記住就好了,忘記了就看看,寫多了說(shuō)不定有一天就會(huì)理解了;
刪除操作
這個(gè)操作調(diào)用的remove方法,這個(gè)有點(diǎn)意思,因?yàn)槟阍贏PI文檔中找不到Properties類有這個(gè)方法,那么就可以看一下源碼:
可以發(fā)現(xiàn)它實(shí)際調(diào)用了別人的remove方法,那么是誰(shuí)的呢?我們?cè)偻驴?#xff1a;
這個(gè)其實(shí)就是ConcurrentHashMap的方法,所以這個(gè)remove方法并沒(méi)有記在Properties類中;
其實(shí)學(xué)會(huì)怎么使用就行了,這個(gè)使用也很簡(jiǎn)單,同樣用代碼示范一下:
package io;import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties;// 實(shí)現(xiàn)對(duì).properties 文件內(nèi)容的刪除 public class PropertiesBlog03 {public void deleteProperties() throws IOException {Properties properties = new Properties();// 將文件數(shù)據(jù)加載到內(nèi)存中properties.load(new FileInputStream("blogtest.properties"));// 通過(guò)鍵值刪除對(duì)應(yīng)的數(shù)據(jù)properties.remove("gender"); // 刪除性別//重置文件中數(shù)據(jù)properties.store(new FileOutputStream("blogtest.properties"), "this is a comment");}public static void main(String[] args) throws IOException {PropertiesBlog03 propertiesBlog03 = new PropertiesBlog03();propertiesBlog03.deleteProperties();} }運(yùn)行后文件變?yōu)?#xff1a;
#this is a comment #Fri Nov 19 21:22:00 CST 2021 name=jack age=18可以看到gender鍵值對(duì)以及刪除了;
總結(jié)
這就是對(duì)Properties的一些基礎(chǔ)介紹,可能在使用時(shí)還會(huì)遇到其他問(wèn)題,可以在評(píng)論區(qū)和我交流,希望這篇文章能夠給你帶來(lái)幫助!!
歡迎大家的點(diǎn)評(píng)!
總結(jié)
以上是生活随笔為你收集整理的[Java基础] Properties类的基本操作和介绍的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [Java基础]Map集合的遍历
- 下一篇: java美元兑换,(Java实现) 美元