Java 對(duì) Properties 文件的操作詳解及簡(jiǎn)單實(shí)例
Java 對(duì) Properties 文件的操作
簡(jiǎn)介
在 Java 中,我們常用 java.util.Properties.Properties 類來(lái)解析 Properties 文件,Properties 格式文件是 Java 常用的配置文件,它用來(lái)在文件中存儲(chǔ)鍵-值對(duì),其中鍵和值用等號(hào)分隔,格式如下:
name=shawearn
Properties 類是 java.util.Hashtable<Object, Object> 的子類,用于鍵和值之間的映射。
在對(duì) Properties 格式文件的操作中,我們常使用 Properties 類的一下方法:
Properties():用于創(chuàng)建一個(gè)無(wú)任何屬性值 Properties 對(duì)象;
- void load(InputStream inStream):從輸入流中加載屬性列表;
- void store(OutputStream out, String comments):根據(jù)輸出流將屬性列表保存到文件中;
- String getProperty(String key):獲取指定鍵的值;
- void setProperty(String key, String value):設(shè)置指定鍵的值,若指定鍵已經(jīng)在原屬性值列表中存在,則覆蓋;若指定鍵在原屬性值列表中不存在,則新增;
寫入 Properties 文件:
// 創(chuàng)建一個(gè) Properties 實(shí)例;
Properties p = new Properties();
// 為 Properties 設(shè)置屬性及屬性值;
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)建一個(gè) Properties 實(shí)例;
Properties p = new Properties();
// 讀取配置文件;
FileInputStream in = new FileInputStream("shawearn.properties");
// 加載配置文件到 Properties 實(shí)例中;
p.load(in);
in.close();
最后附上測(cè)試代碼:
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();
// 測(cè)試寫入;
t.testWrite();
// 測(cè)試讀取;
t.testRead();
}
/*
* 測(cè)試對(duì) Properties 文件的寫入操作;
*/
private void testWrite() throws IOException {
// 創(chuàng)建一個(gè) Properties 實(shí)例;
Properties p = new Properties();
// 為 Properties 設(shè)置屬性及屬性值;
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("寫入成功!");
}
/*
* 測(cè)試對(duì) Properties 文件的讀取操作;
*/
private void testRead() throws IOException {
// 創(chuàng)建一個(gè) Properties 實(shí)例;
Properties p = new Properties();
// 讀取配置文件;
FileInputStream in = new FileInputStream("shawearn.properties");
// 加載配置文件到 Properties 實(shí)例中;
p.load(in);
in.close();
// 獲取 Properties 文件中所有的 key;
Set<String> keys = p.stringPropertyNames();
// 遍歷所有的 key;
for (String key : keys) {
// 獲取 Properties 文件中 key 所對(duì)應(yīng)的 value;
Object value = p.get(key);
// 輸入 key 和對(duì)應(yīng)的 value;
System.out.println(key + " => " + value);
}
}
}
控制臺(tái)輸出結(jié)果:
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
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Spring lazy-init 懶加載的原理小結(jié)
lazy-init 是一個(gè)非常重要的屬性,可以優(yōu)化應(yīng)用的啟動(dòng)時(shí)間,尤其是在處理大量bean或者復(fù)雜依賴關(guān)系時(shí),可以顯著提高應(yīng)用的響應(yīng)速度,本文主要介紹了Spring lazy-init 懶加載的原理小結(jié),感興趣的可以了解一下2025-04-04
JavaFX程序初次運(yùn)行創(chuàng)建數(shù)據(jù)庫(kù)并執(zhí)行建表SQL詳解
這篇文章主要介紹了JavaFX程序初次運(yùn)行創(chuàng)建數(shù)據(jù)庫(kù)并執(zhí)行建表SQL詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
spring boot使用logback實(shí)現(xiàn)多環(huán)境日志配置詳解
這篇文章主要介紹了spring boot使用logback實(shí)現(xiàn)多環(huán)境日志配置詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
解決springboot使用logback日志出現(xiàn)LOG_PATH_IS_UNDEFINED文件夾的問(wèn)題
這篇文章主要介紹了解決springboot使用logback日志出現(xiàn)LOG_PATH_IS_UNDEFINED文件夾的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Java基于HttpClient實(shí)現(xiàn)RPC的示例
HttpClient可以實(shí)現(xiàn)使用Java代碼完成標(biāo)準(zhǔn)HTTP請(qǐng)求及響應(yīng)。本文主要介紹了Java基于HttpClient實(shí)現(xiàn)RPC,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10

