Java實現(xiàn)讀取和寫入properties文件
Java讀取和寫入properties文件
properties文件是一種屬性文件,這種文件以key=value格式存儲內(nèi)容。
Java中可以使用Properties類來讀取這個文件,一般properties文件作為一些參數(shù)的存儲,使得代碼更加靈活。
這里先定義一個data.properties文件,內(nèi)容如下:
cn=12 kr=14 jp=64
既然properties是一個文件,那么我們也可以用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();
}
運行結(jié)果
// cn=12
// kr=14
// jp=64但是,由于properties文件中每一行都是獨立的鍵值對,這種普通讀取方式并沒有按照鍵值對的方式進(jìn)行讀取,而是逐個字節(jié)讀取,對于這種文件來講是沒有意義的。
所以就應(yīng)該使用Properties類來進(jìn)行讀取,從jdk源碼中可以看出,Properties繼承自Hashtable,因此,Properties也同樣有put和get方法,由于,properties是一個文件,Properties類提供了一個load()方法,InputStram作為數(shù)據(jù)源,傳入一個輸入流,把輸入流中的內(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集合對象中
// 根據(jù)key,獲取value
System.out.println("cn");
} catch (IOException e) {
e.printStackTrace();
}
// 運行結(jié)果
// cn=12同樣,在寫properties文件時,Properties類也提供了store()方法,OutputStream和String類型的注釋作為數(shù)據(jù)源,將內(nèi)部的鍵值對集合和注釋傳入到輸出流中,就可以進(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鍵值對,寫入*.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)建時間和每對鍵值對 。
Java讀寫properties文件(java.util.Properties)
Java對于properties文件的讀寫可以說是最簡單的一個讀取、寫入配置文件的方法了,在properties文件中,數(shù)據(jù)是用類似于鍵值對的存儲方式進(jìn)行存儲的。
下面就是一個簡單的properties文件:
username=xm99 password=1234567
沒錯,就是這么簡單的方式。
那么我們想要對properties文件進(jìn)行操作的時候應(yīng)該如何操作呢?
- 生成一個properties對象
- 使用properties對象的
String name = p.getProperty("valueName");方法獲得配置的值 - 使用properties對象的
setProperty(String key,String value)方法在配置文件中增加配置信息。之后使用store()方法進(jìn)行保存 - 關(guān)閉輸入流/輸出流對象
寫入配置文件代碼實例
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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
JPA @Query時,無法使用limit函數(shù)的問題及解決
這篇文章主要介紹了JPA @Query時,無法使用limit函數(shù)的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Idea中SpringBoot多模塊項目的建立實現(xiàn)
這篇文章主要介紹了Idea中SpringBoot多模塊項目的建立實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Java(Springboot)項目調(diào)用第三方WebService接口實現(xiàn)代碼
這篇文章主要介紹了如何使用Java調(diào)用WebService接口,傳遞XML參數(shù),獲取XML響應(yīng),并將其解析為JSON格式,文中詳細(xì)描述了WSDL文檔的使用、HttpClientBuilder和Apache?Axis兩種調(diào)用方式的具體實現(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ì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
詳解Spring系列之@ComponentScan批量注冊bean
本文介紹各種@ComponentScan批量掃描注冊bean的基本使用以及進(jìn)階用法和@Componet及其衍生注解使用,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2022-02-02

