快速上手Java中的Properties集合類
概念
Java中的Properties文件是一種配置文件,主要用于表達(dá)配置信息,格式是文本文件。該類主要用于讀取Java的配置文件,也可以對(duì)properties文件進(jìn)行修改
屬性配置:以“鍵=值”的方式書寫一個(gè)屬性的配置信息
注 釋:在properties文件中,可以用“#”來注釋

為什么需要Properties類?
將一些固定修改的內(nèi)容放到Properties文件,如果我們將這些內(nèi)容放到程序里面(比如:賬號(hào)、密碼),假如說這些內(nèi)容需要改變,意味著需要對(duì)源程序進(jìn)行修改(修改源碼);當(dāng)再次運(yùn)行的時(shí)候,需要進(jìn)行重新編譯或運(yùn)行在第三方(當(dāng)程序工程量大的時(shí)候,會(huì)浪費(fèi)大量資源,靈活性差)
Properties類可以很輕松的理解和修改它們
傳統(tǒng)的方法(不使用Properties類)
split方法是String類中,用于分割字符串,將分割的字符串變成字符串?dāng)?shù)組
test.Properties文件的內(nèi)容
ip=192.168.1.100 user=root pwd=123456
讀取test.properties文件,并獲取相應(yīng)的ip user pwd
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Properties01 {
public static void main(String[] args) {
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader("src\\test.properties"));
String line = "";
while ((line = bufferedReader.readLine()) !=null){
String[] split = line.split("=");
System.out.println(split[0]+"值是:"+split[1]);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
控制臺(tái)輸出如下:
ip值是:192.168.1.100
user值是:root
pwd值是:123456
傳統(tǒng)方法解決并不是很方便,需要遍歷循環(huán),如果要獲取文件中指定的某個(gè)內(nèi)容,這時(shí)候需要用到Properties類
Properties
Properties 繼承于 Hashtable。表示一個(gè)持久的屬性集.屬性列表中每個(gè)鍵及其對(duì)應(yīng)值都是一個(gè)字符串。
- 專門用于讀取配置文件的集合類
配置文件格式:
鍵=值
- 鍵=值鍵值對(duì)不需要由空格,值不需要用引號(hào)括起來,默認(rèn)類型是String

該類提供了兩個(gè)構(gòu)造器

常見Properties調(diào)用方法
- load:加載配置文件的鍵值對(duì)到Properties對(duì)象

- list:將數(shù)據(jù)顯示到指定設(shè)備

- getProperties(key):根據(jù)鍵獲取值
- setProperties(key,value):設(shè)置鍵值對(duì)到Properties對(duì)象
- store:將Properties中的鍵值對(duì)存儲(chǔ)到配置文件中(如果已存在則覆蓋),在IDEA編譯器中,保存信息到配置文件,如果含有中文,會(huì)存儲(chǔ)Unicode編碼格式

更多具體方法可在JDK文檔或百度進(jìn)行查看
示例
1.演示使用Properties類讀取test.properties文件的內(nèi)容
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class Properties02 {
public static void main(String[] args) {
//創(chuàng)建Properties對(duì)象
Properties properties = new Properties();
//加載指定的配置文件
try {
properties.load(new FileReader("src\\test.properties"));
//把鍵值對(duì)顯示到控制臺(tái)
properties.list(System.out);
System.out.println("===根據(jù)鍵獲取對(duì)應(yīng)的值===");
System.out.println(properties.getProperty("ip"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
– listing properties –
user=root
pwd=123456
ip=192.168.1.100
=根據(jù)鍵獲取對(duì)應(yīng)的值=
192.168.1.100
2.使用Properties類添加鍵值對(duì)到文件test.properties中
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class Properties03 {
public static void main(String[] args) {
Properties properties = new Properties();
properties.setProperty("charset", "UTF-8");
properties.setProperty("user", "jack");
properties.setProperty("pwd", "123456");
try {
properties.store(new FileWriter("src\\test.properties"), null);
//第二個(gè)參數(shù),表示注釋信息(null表示沒有注釋)
System.out.println("保存配置文件成功");
} catch (IOException e) {
e.printStackTrace();
}
}
}
此時(shí)test.properties文件的內(nèi)容為
#Tue Jan 17 18:31:36 CST 2023
user=jack
pwd=123456
charset=UTF-8
注意:如果保存的內(nèi)容是中文,則存放的格式是Unicode編碼
3.完成對(duì)test.properties文件的讀取,并修改某個(gè)鍵值對(duì)
注意:使用setProperty方法,如果key不存在則創(chuàng)建,反之存在,則修改
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class Properties04 {
public static void main(String[] args) {
Properties properties = new Properties();
properties.setProperty("charset", "UTF-8");
properties.setProperty("user", "jack");
properties.setProperty("pwd", "123456");
properties.setProperty("user", "tom");
properties.setProperty("pwd", "888888");
try {
properties.store(new FileWriter("src\\test.properties"), null);
System.out.println("保存配置文件成功");
} catch (IOException e) {
e.printStackTrace();
}
}
}
此時(shí)文件內(nèi)容為:
#Tue Jan 17 18:45:04 CST 2023
user=tom
pwd=888888
charset=UTF-8
setProperty操作時(shí)底層源碼(父類Hashtable中的方法,可忽略)

public synchronized V put(K key, V value) {
// Make sure the value is not null
if (value == null) {
throw new NullPointerException();
}
// Makes sure the key is not already in the hashtable.
Entry<?,?> tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry<K,V> entry = (Entry<K,V>)tab[index];
for(; entry != null ; entry = entry.next) {
if ((entry.hash == hash) && entry.key.equals(key)) {
V old = entry.value;
entry.value = value;
return old;
}
}
addEntry(hash, key, value, index);
return null;
}
總結(jié)
到此這篇關(guān)于Java中Properties集合類的文章就介紹到這了,更多相關(guān)Java Properties集合類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring IOC創(chuàng)建對(duì)象的兩種方式
這篇文章主要給大家介紹了關(guān)于Spring IOC創(chuàng)建對(duì)象的兩種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
java中對(duì)Redis的緩存進(jìn)行操作的示例代碼
本篇文章主要介紹了java中對(duì)Redis的緩存進(jìn)行操作的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
@SpringBootConfiguration重復(fù)加載報(bào)錯(cuò)問題解決
@SpringBootApplication?注解的?exclude?屬性用于排除特定的自動(dòng)配置類,而不是用于排除主配置類本身,因此,不能通過?exclude?屬性來排除主配置類的加載,這篇文章主要介紹了@SpringBootConfiguration重復(fù)加載報(bào)錯(cuò),需要的朋友可以參考下2024-08-08
java多線程編程之慎重使用volatile關(guān)鍵字
volatile關(guān)鍵字相信了解Java多線程的讀者都很清楚它的作用。volatile關(guān)鍵字用于聲明簡(jiǎn)單類型變量,下面看一下為什么要慎重使用volatile關(guān)鍵字2014-01-01
SpringBoot封裝響應(yīng)數(shù)據(jù)實(shí)現(xiàn)過程詳解
這篇文章主要介紹了SpringBoot封裝響應(yīng)數(shù)據(jù)實(shí)現(xiàn)過程,SpringBoot響應(yīng)數(shù)據(jù)封裝是指在SpringBoot應(yīng)用程序中,將返回的數(shù)據(jù)進(jìn)行封裝,以便于前端頁面或其他客戶端使用,感興趣想要詳細(xì)了解可以參考下文2023-05-05
IntelliJ?IDEA?2023.1.4?無法刷新Maven項(xiàng)目模塊的問題及解決方法
這篇文章主要介紹了如何排查?IDEA?自身報(bào)錯(cuò)問題,本文以IntelliJ?IDEA?2023.1.4無法刷新項(xiàng)目Maven模塊的問題為例,給大家詳細(xì)講解,需要的朋友可以參考下2023-08-08
idea中g(shù)it如何修改commit(ChangeList的使用)
這篇文章主要介紹了idea中g(shù)it如何修改commit(ChangeList的使用),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04

