Java 對 Properties 文件的操作詳解及簡單實例
Java 對 Properties 文件的操作
簡介
在 Java 中,我們常用 java.util.Properties.Properties 類來解析 Properties 文件,Properties 格式文件是 Java 常用的配置文件,它用來在文件中存儲鍵-值對,其中鍵和值用等號分隔,格式如下:
name=shawearn
Properties 類是 java.util.Hashtable<Object, Object> 的子類,用于鍵和值之間的映射。
在對 Properties 格式文件的操作中,我們常使用 Properties 類的一下方法:
Properties():用于創(chuàng)建一個無任何屬性值 Properties 對象;
- void load(InputStream inStream):從輸入流中加載屬性列表;
- void store(OutputStream out, String comments):根據(jù)輸出流將屬性列表保存到文件中;
- String getProperty(String key):獲取指定鍵的值;
- void setProperty(String key, String value):設置指定鍵的值,若指定鍵已經(jīng)在原屬性值列表中存在,則覆蓋;若指定鍵在原屬性值列表中不存在,則新增;
寫入 Properties 文件:
// 創(chuàng)建一個 Properties 實例;
Properties p = new Properties();
// 為 Properties 設置屬性及屬性值;
p.setProperty("name", "shawearn");
p.setProperty("address", "XX 省 XX 市");
// 保存 Properties 到 shawearn.properties 文件中;
FileOutputStream out = new FileOutputStream("shawearn.properties");
p.store(out, "Create by Shawearn!");
out.close();
讀取 Properties 文件:
// 創(chuàng)建一個 Properties 實例;
Properties p = new Properties();
// 讀取配置文件;
FileInputStream in = new FileInputStream("shawearn.properties");
// 加載配置文件到 Properties 實例中;
p.load(in);
in.close();
最后附上測試代碼:
package com.shawearn.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
/**
* @author Shawearn
*
*/
public class TestProperties {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
TestProperties t = new TestProperties();
// 測試寫入;
t.testWrite();
// 測試讀??;
t.testRead();
}
/*
* 測試對 Properties 文件的寫入操作;
*/
private void testWrite() throws IOException {
// 創(chuàng)建一個 Properties 實例;
Properties p = new Properties();
// 為 Properties 設置屬性及屬性值;
p.setProperty("name", "shawearn");
p.setProperty("address", "XX 省 XX 市");
// 保存 Properties 到 shawearn.properties 文件中;
FileOutputStream out = new FileOutputStream("shawearn.properties");
p.store(out, "Create by Shawearn!");
out.close();
System.out.println("寫入成功!");
}
/*
* 測試對 Properties 文件的讀取操作;
*/
private void testRead() throws IOException {
// 創(chuàng)建一個 Properties 實例;
Properties p = new Properties();
// 讀取配置文件;
FileInputStream in = new FileInputStream("shawearn.properties");
// 加載配置文件到 Properties 實例中;
p.load(in);
in.close();
// 獲取 Properties 文件中所有的 key;
Set<String> keys = p.stringPropertyNames();
// 遍歷所有的 key;
for (String key : keys) {
// 獲取 Properties 文件中 key 所對應的 value;
Object value = p.get(key);
// 輸入 key 和對應的 value;
System.out.println(key + " => " + value);
}
}
}
控制臺輸出結果:
address => XX 省 XX 市 name => shawearn
shawearn.properties 文件內(nèi)容:
#Create by Shawearn! #Thu Nov 19 12:43:41 CST 2015 name=shawearn address=XX \u7701 XX \u5E02
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
SpringBoot使用WebSocket實現(xiàn)向前端推送消息功能
WebSocket協(xié)議是基于TCP的一種新的網(wǎng)絡協(xié)議,它實現(xiàn)了瀏覽器與服務器全雙工(full-duplex)通信——允許服務器主動發(fā)送信息給客戶端,本文給大家介紹了SpringBoot使用WebSocket實現(xiàn)向前端推送消息功能,需要的朋友可以參考下2024-05-05
java?中的HashMap的底層實現(xiàn)和元素添加流程
這篇文章主要介紹了java?中的HashMap的底層實現(xiàn)和元素添加流程,HashMap?是使用頻率最高的數(shù)據(jù)類型之一,同時也是面試必問的問題之一,尤其是它的底層實現(xiàn)原理,下文更多詳細內(nèi)容,需要的小伙伴可以參考一下2022-05-05
關于服務網(wǎng)關Spring Cloud Zuul(Finchley版本)
這篇文章主要介紹了關于服務網(wǎng)關Spring Cloud Zuul(Finchley版本),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
JAVA Spring Boot 自動配置實現(xiàn)原理詳解
這篇文章主要介紹了詳解SpringBoot自動配置原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2021-09-09
Java 確保某個Bean類被最后執(zhí)行的幾種實現(xiàn)方式
這篇文章主要介紹了Java 確保某個BeanDefinitionRegistryPostProcessor Bean被最后執(zhí)行的幾種實現(xiàn)方式,幫助大家更好的理解和學習使用Java,感興趣的朋友可以了解下2021-03-03

