Java 對(duì) Properties 文件的操作詳解及簡(jiǎn)單實(shí)例
Java 對(duì) Properties 文件的操作
簡(jiǎn)介
在 Java 中,我們常用 java.util.Properties.Properties 類(lèi)來(lái)解析 Properties 文件,Properties 格式文件是 Java 常用的配置文件,它用來(lái)在文件中存儲(chǔ)鍵-值對(duì),其中鍵和值用等號(hào)分隔,格式如下:
name=shawearn
Properties 類(lèi)是 java.util.Hashtable<Object, Object> 的子類(lèi),用于鍵和值之間的映射。
在對(duì) Properties 格式文件的操作中,我們常使用 Properties 類(lèi)的一下方法:
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)在原屬性值列表中存在,則覆蓋;若指定鍵在原屬性值列表中不存在,則新增;
寫(xiě)入 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è)試寫(xiě)入; t.testWrite(); // 測(cè)試讀??; t.testRead(); } /* * 測(cè)試對(duì) Properties 文件的寫(xiě)入操作; */ 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("寫(xiě)入成功!"); } /* * 測(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ì)本站的支持!
- Java讀取.properties配置文件方法示例
- Java 讀取、獲取配置文件.properties中的數(shù)據(jù)
- Java讀寫(xiě).properties文件解決中文亂碼問(wèn)題
- Java實(shí)現(xiàn)的properties文件動(dòng)態(tài)修改并自動(dòng)保存工具類(lèi)
- java web開(kāi)發(fā)中獲取tomcat上properties文件內(nèi)容的方法
- java加載properties文件的六種方法總結(jié)
- Java中的幾種讀取properties配置文件的方式
- Java加載properties文件實(shí)現(xiàn)方式詳解
相關(guān)文章
SpringBoot使用WebSocket實(shí)現(xiàn)向前端推送消息功能
WebSocket協(xié)議是基于TCP的一種新的網(wǎng)絡(luò)協(xié)議,它實(shí)現(xiàn)了瀏覽器與服務(wù)器全雙工(full-duplex)通信——允許服務(wù)器主動(dòng)發(fā)送信息給客戶(hù)端,本文給大家介紹了SpringBoot使用WebSocket實(shí)現(xiàn)向前端推送消息功能,需要的朋友可以參考下2024-05-05java?中的HashMap的底層實(shí)現(xiàn)和元素添加流程
這篇文章主要介紹了java?中的HashMap的底層實(shí)現(xiàn)和元素添加流程,HashMap?是使用頻率最高的數(shù)據(jù)類(lèi)型之一,同時(shí)也是面試必問(wèn)的問(wèn)題之一,尤其是它的底層實(shí)現(xiàn)原理,下文更多詳細(xì)內(nèi)容,需要的小伙伴可以參考一下2022-05-05MyBatis-Plus 自動(dòng)填充的實(shí)現(xiàn)示例
MyBatis-Plus 提供了自動(dòng)填充功能,幫助開(kāi)發(fā)者在插入或更新數(shù)據(jù)時(shí),自動(dòng)為某些字段賦值,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-09-09關(guān)于服務(wù)網(wǎng)關(guān)Spring Cloud Zuul(Finchley版本)
這篇文章主要介紹了關(guān)于服務(wù)網(wǎng)關(guān)Spring Cloud Zuul(Finchley版本),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03JAVA Spring Boot 自動(dòng)配置實(shí)現(xiàn)原理詳解
這篇文章主要介紹了詳解SpringBoot自動(dòng)配置原理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2021-09-09Java 確保某個(gè)Bean類(lèi)被最后執(zhí)行的幾種實(shí)現(xiàn)方式
這篇文章主要介紹了Java 確保某個(gè)BeanDefinitionRegistryPostProcessor Bean被最后執(zhí)行的幾種實(shí)現(xiàn)方式,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-03-03使用SpringMVC返回json字符串的實(shí)例講解
下面小編就為大家分享一篇使用SpringMVC返回json字符串的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03