Java IO流之Properties類(lèi)的使用
前言
Properties類(lèi)的基本介紹

Properties類(lèi)的常見(jiàn)方法

使用Properties類(lèi)來(lái)讀取配置文件mysql.properties
mysql.properties配置文件中具體內(nèi)容如下:

/**
* 使用Properties類(lèi)來(lái)讀取mysql.properties文件
*/
public class Properties02 {
public static void main(String[] args) throws IOException {
//1.創(chuàng)建Properties對(duì)象
Properties properties = new Properties();
properties.load(new FileReader("src\\mysql.properties"));
//3.把鍵值對(duì) 顯示到控制臺(tái)
properties.list(System.out);
System.out.println("--------------------")
//根據(jù)key 獲取對(duì)應(yīng)的值
String pwd = properties.getProperty("pwd");
String user = properties.getProperty("user");
System.out.println("用戶(hù)名:" + user);
System.out.println("密碼:" + pwd);
}
}結(jié)果如下:
-- listing properties --
user=root
pwd=123456
ip=192.168.100.100
--------------------
用戶(hù)名:root
密碼:123456
使用Properties類(lèi)創(chuàng)建配置文件,修改配置文件內(nèi)容
具體代碼如下:
/**
* 使用Properties類(lèi) 創(chuàng)建配置文件,修改配置文件內(nèi)容
*/
public class Properties03 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
//創(chuàng)建
//1.如果該文件沒(méi)有key,就是創(chuàng)建
//2.如果改文件有key,就是修改
/*
Properties父類(lèi)就是Hashtable,底層就是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;//如果key存在,就替換
return old;
}
}
addEntry(hash, key, value, index); //如果是新key,就添加
return null;
}
*/
properties.setProperty("charset", "utf8");
properties.setProperty("user", "湯姆");
properties.setProperty("pwd", "888");
//store()方法的第二個(gè)參數(shù),表示注釋
properties.store(new FileOutputStream("src\\mysql2.properties"), "hello");
System.out.println("保存配置文件成功!");
}
}創(chuàng)建的配置文件如下:

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Servlet開(kāi)發(fā)JavaWeb工程示例詳解
這篇文章主要介紹了Servlet開(kāi)發(fā)JavaWeb工程示例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
java多線程編程同步器Future和FutureTask解析及代碼示例
這篇文章主要介紹了java多線程編程同步器Future和FutureTask解析及代碼示例,對(duì)二者進(jìn)行了詳細(xì)介紹,分析了future的源碼,最后展示了相關(guān)實(shí)例代碼,具有一定參考價(jià)值 ,需要的朋友可以了解下。2017-11-11
詳解基于spring多數(shù)據(jù)源動(dòng)態(tài)調(diào)用及其事務(wù)處理
本篇文章主要介紹了基于spring多數(shù)據(jù)源動(dòng)態(tài)調(diào)用及其事務(wù)處理 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
從ElasticSearch中刪除數(shù)據(jù)的幾種常見(jiàn)方式
這篇文章主要給大家介紹了關(guān)于從ElasticSearch中刪除數(shù)據(jù)的幾種常見(jiàn)方式,在Elasticsearch中刪除數(shù)據(jù)可以通過(guò)刪除索引或刪除文檔兩種方式實(shí)現(xiàn),需要的朋友可以參考下2024-10-10
深入了解SpringBoot中@InitBinder注解的使用
這篇文章主要介紹了深入了解SpringBoot中@InitBinder注解的使用,@InitBinder注解可以作用在被@Controller注解的類(lèi)的方法上,表示為當(dāng)前控制器注冊(cè)一個(gè)屬性編輯器,用于對(duì)WebDataBinder進(jìn)行初始化,且只對(duì)當(dāng)前的Controller有效,需要的朋友可以參考下2023-10-10
SpringBoot使用JPA實(shí)現(xiàn)查詢(xún)部分字段
這篇文章主要介紹了SpringBoot使用JPA實(shí)現(xiàn)查詢(xún)部分字段方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

