本篇文章為大家展示了如何使用Java實(shí)現(xiàn)操作 Properties 文件,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)富川免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
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ì)象;
寫入 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
上述內(nèi)容就是如何使用Java實(shí)現(xiàn)操作 Properties 文件,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享標(biāo)題:如何使用Java實(shí)現(xiàn)操作Properties文件
文章起源:http://www.chinadenli.net/article36/gshgpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、標(biāo)簽優(yōu)化、電子商務(wù)、網(wǎng)站制作、用戶體驗(yàn)、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)