Java實(shí)現(xiàn)讀取和寫入properties文件
Java讀取和寫入properties文件
properties文件是一種屬性文件,這種文件以key=value格式存儲(chǔ)內(nèi)容。
Java中可以使用Properties類來讀取這個(gè)文件,一般properties文件作為一些參數(shù)的存儲(chǔ),使得代碼更加靈活。
這里先定義一個(gè)data.properties文件,內(nèi)容如下:
cn=12 kr=14 jp=64
既然properties是一個(gè)文件,那么我們也可以用FileInputStreram來進(jìn)行讀?。?/p>
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\test\\data.properties"))) {
int data = -1;
while((data = bis.read()) != -1) {
System.out.print((char)data);
}
} catch (IOException e) {
e.printStackTrace();
}
運(yùn)行結(jié)果
// cn=12
// kr=14
// jp=64但是,由于properties文件中每一行都是獨(dú)立的鍵值對(duì),這種普通讀取方式并沒有按照鍵值對(duì)的方式進(jìn)行讀取,而是逐個(gè)字節(jié)讀取,對(duì)于這種文件來講是沒有意義的。
所以就應(yīng)該使用Properties類來進(jìn)行讀取,從jdk源碼中可以看出,Properties繼承自Hashtable,因此,Properties也同樣有put和get方法,由于,properties是一個(gè)文件,Properties類提供了一個(gè)load()方法,InputStram作為數(shù)據(jù)源,傳入一個(gè)輸入流,把輸入流中的內(nèi)容傳入到內(nèi)部的key和value中,然后就能讀取到properties文件中的內(nèi)容了。
代碼如下:
// Properties格式文件的寫入
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\test\\data.properties"))) {
Properties props = new Properties();
props.load(bis); // 將“輸入流”加載至Properties集合對(duì)象中
// 根據(jù)key,獲取value
System.out.println("cn");
} catch (IOException e) {
e.printStackTrace();
}
// 運(yùn)行結(jié)果
// cn=12同樣,在寫properties文件時(shí),Properties類也提供了store()方法,OutputStream和String類型的注釋作為數(shù)據(jù)源,將內(nèi)部的鍵值對(duì)集合和注釋傳入到輸出流中,就可以進(jìn)行寫入操作了。
代碼如下:
// Properties格式文件的寫入
try {
Properties props = new Properties();
props.put("F1", "2314");
props.put("F2", "2341");
props.put("F3", "5322");
props.put("F4", "4316");
// 使用“輸出流”,將Properties集合中的KV鍵值對(duì),寫入*.properties文件
try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\test\\demo.properties"))){
props.store(bos, "just do IT");
}
} catch (IOException e) {
e.printStackTrace();
}
可以看到,demo.properties文件已經(jīng)被創(chuàng)建出來了,內(nèi)容包括注釋、創(chuàng)建時(shí)間和每對(duì)鍵值對(duì) 。
Java讀寫properties文件(java.util.Properties)
Java對(duì)于properties文件的讀寫可以說是最簡單的一個(gè)讀取、寫入配置文件的方法了,在properties文件中,數(shù)據(jù)是用類似于鍵值對(duì)的存儲(chǔ)方式進(jìn)行存儲(chǔ)的。
下面就是一個(gè)簡單的properties文件:
username=xm99 password=1234567
沒錯(cuò),就是這么簡單的方式。
那么我們想要對(duì)properties文件進(jìn)行操作的時(shí)候應(yīng)該如何操作呢?
- 生成一個(gè)properties對(duì)象
- 使用properties對(duì)象的
String name = p.getProperty("valueName");方法獲得配置的值 - 使用properties對(duì)象的
setProperty(String key,String value)方法在配置文件中增加配置信息。之后使用store()方法進(jìn)行保存 - 關(guān)閉輸入流/輸出流對(duì)象
寫入配置文件代碼實(shí)例
package proTest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class proSavaTest {
public static void main(String[] args) throws IOException {
FileOutputStream out = new FileOutputStream("D:\\demo\\javaBase\\src\\proTest\\pro.properties");
Properties pro = new Properties();
pro.setProperty("username","xm99");
pro.setProperty("passwd","12345");
pro.store(out,"acb");
}
}讀取配置文件示例
package proTest;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class proTest {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("D:\\demo\\javaBase\\src\\proTest\\pro.properties");
Properties pro = new Properties();
pro.load(in);
String username = pro.getProperty("username");
String passwd = pro.getProperty("passwd");
System.out.println("username="+username);
System.out.println("passwd="+passwd);
in.close();
}
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
JPA @Query時(shí),無法使用limit函數(shù)的問題及解決
這篇文章主要介紹了JPA @Query時(shí),無法使用limit函數(shù)的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Idea中SpringBoot多模塊項(xiàng)目的建立實(shí)現(xiàn)
這篇文章主要介紹了Idea中SpringBoot多模塊項(xiàng)目的建立實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Java(Springboot)項(xiàng)目調(diào)用第三方WebService接口實(shí)現(xiàn)代碼
這篇文章主要介紹了如何使用Java調(diào)用WebService接口,傳遞XML參數(shù),獲取XML響應(yīng),并將其解析為JSON格式,文中詳細(xì)描述了WSDL文檔的使用、HttpClientBuilder和Apache?Axis兩種調(diào)用方式的具體實(shí)現(xiàn)步驟,需要的朋友可以參考下2025-02-02
Java JSON轉(zhuǎn)成List結(jié)構(gòu)數(shù)據(jù)
這篇文章主要介紹了Java JSON轉(zhuǎn)成List結(jié)構(gòu)數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
詳解Spring系列之@ComponentScan批量注冊(cè)bean
本文介紹各種@ComponentScan批量掃描注冊(cè)bean的基本使用以及進(jìn)階用法和@Componet及其衍生注解使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2022-02-02
Spring的@CrossOrigin注解處理請(qǐng)求源碼解析
這篇文章主要介紹了Spring的@CrossOrigin注解處理請(qǐng)求源碼解析,@CrossOrigin源碼解析主要分為兩個(gè)階段@CrossOrigin注釋的方法掃描注冊(cè),請(qǐng)求匹配@CrossOrigin注釋的方法,本文從源碼角度進(jìn)行解析,需要的朋友可以參考下2023-12-12

