Java如何讀寫Properties配置文件(Properties類)
Java讀寫Properties配置文件
Properties基本介紹
專門用來讀寫配置文件的集合類,Properties類表示一組持久的屬性。 Properties可以保存到流中或從流中加載。 屬性列表中的每個鍵及其對應(yīng)的值都是一個字符串。
屬性列表可以包含另一個屬性列表作為其“默認(rèn)值”; 如果在原始屬性列表中找不到屬性鍵,則會搜索此第二個屬性列表。
因為Properties從繼承Hashtable時, put種putAll方法可應(yīng)用于Properties對象。 強(qiáng)烈不鼓勵使用它們,因為它們允許調(diào)用者插入其鍵或值不是Strings 。 應(yīng)該使用setProperty方法。
如果store或save方法在包含非String鍵或值的“受損害” Properties對象上調(diào)用,則調(diào)用將失敗。
類似地,如果在包含非String密鑰的“受損害” Properties對象上調(diào)用propertyNames或list方法的調(diào)用將失敗。
配置文件的格式:鍵 = 值(key = value)
PS:鍵值對不需要有空格,值不需要用引號包起來。默認(rèn)類型為String
properties是配置文件。
主要的作用是通過修改配置文件可以方便地修改代碼中的參數(shù),實現(xiàn)不用改class文件即可靈活變更參數(shù)。
解釋:java運行中java文件會變成class文件,之后無法通過反編譯找到原樣的代碼,這樣的話,如果java類中某個參數(shù)變更,就很難靈活的實現(xiàn)參數(shù)修改,這個時候properties 文件就能很靈活的實現(xiàn)配置,減少代碼的維護(hù)成本和提高開發(fā)效率。
Properties常用方法
load()//加載配置文件的鍵值對到Properties對象中list()//將數(shù)據(jù)顯示到指定位置getProperty(key)//根據(jù)鍵獲取值setProperty(key,value)//設(shè)置鍵值對到Properties對象中store()//將Properties中的鍵值對存儲到配置文件之后,在idea中,保存信息到配置文件,如果含有中文,會存儲為unicode碼loadFromXML(InputStream)和storeToXML(OutputStream, String, String)//方法以簡單的XML格式加載和存儲屬性。 默認(rèn)情況下,使用UTF-8字符編碼,但是如果需要,可以指定特定編碼。 需要實現(xiàn)支持UTF-8和UTF-16,并可能支持其他編碼。
XML屬性文檔具有以下DOCTYPE聲明:
Properties讀取配置文件
使用Properties類來讀取test.properties文件
- 1.創(chuàng)建Properties對象
- 2.加載指定配置文件
- 3.把鍵值對(k=v)顯示在控制臺
- 4.根據(jù)key獲取對應(yīng)的value值
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class Test01 {
public static void main(String[] args) throws IOException {
//創(chuàng)建Properties對象
Properties properties = new Properties();
//加載配置文件
properties.load(new FileReader("E:\\Java_基礎(chǔ)\\code\\IO流\\src\\test.properties"));
//將內(nèi)容顯示在控制臺
properties.list(System.out);
//通過key獲取value
String user = properties.getProperty("user");
String password = properties.getProperty("password");
System.out.println("用戶名:"+user);
System.out.println("密碼:"+password);
}
}
Properties寫入配置文件
使用Properties來創(chuàng)建配置文件并設(shè)置內(nèi)容
- 1.創(chuàng)建Properties對象
- 2.創(chuàng)建對應(yīng)的鍵值對
setProperty(String key,String value) - 3.將鍵值對存儲到指定的配置文件中
store(Writer writer, String comments)
PS:如果文件中沒有key,就是創(chuàng)建一對鍵值對,如果該文件有key,就是修改該鍵對應(yīng)的值
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class Test02 {
public static void main(String[] args) throws IOException {
//創(chuàng)建對象
Properties properties = new Properties();
//設(shè)置鍵值對
properties.setProperty("user","root");
properties.setProperty("password","abcdef");
//寫入到指定文件,如果沒有,就創(chuàng)建該文件
properties.store(new FileWriter("E:\\Java_基礎(chǔ)\\code\\IO流\\src\\demo.properties"),null);
//如果comments參數(shù)不為空,則首先將ASCII #字符,注釋字符串和行分隔符寫入輸出流。 因此, comments可以作為識別評論。
//注釋行始終寫入,由ASCII #組成,當(dāng)前日期和時間(如當(dāng)前時間Date的toString方法生成的)以及由Writer生成的行分隔Writer 。
}
}
properties文件的讀取和寫入
properties文件是一種屬性文件,這種文件以key=value(鍵值對)格式存儲內(nèi)容。Java中可以使用Properties類來讀取這個文件,使用Properties類中的getProperties(key)方法來得到對應(yīng)的數(shù)據(jù)。
一般properties文件作為一些參數(shù)的存儲,使得代碼更加靈活。
properties文件的讀取
(1)使用BufferedInputStream(緩沖輸入流)創(chuàng)建輸入流進(jìn)行讀取,讀取后以key=value(鍵值對)格式存儲數(shù)據(jù)。
代碼實現(xiàn):
?//Properties格式文件的讀取
?? ?//創(chuàng)建輸入流
?? ?try (
?? ? BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\student\\aaa.properties"))) {
?? ??? ?int data =-1;
?? ??? ?while((data=bis.read())!=-1) {
?? ??? ??? ?System.out.print((char)data);
?? ??? ?}
?? ?} catch (IOException e) {
?? ??? ?// TODO Auto-generated catch block
?? ??? ?e.printStackTrace();
?? ?}
?? ?//運行結(jié)果
?? ?//k=12
? ? //b=34
?? ?//c=11(2)使用Properties類中的load()方法讀取,將“輸入流”加載到Properties集合對象中,根據(jù)key來獲取value的值
代碼實現(xiàn):
try (
?? ? BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\student\\aaa.properties"))) {
?? ??? ?Properties props=new Properties();
?? ??? ? ?props.load(bis);//將“輸入流”加載到Properties集合對象中
?? ??? ? ? ? ? //根據(jù)key,獲取value
?? ??? ??? ??? ?System.out.println(props.get("k"));
?? ?} catch (IOException e) {
?? ??? ?// TODO Auto-generated catch block
?? ??? ?e.printStackTrace();
?? ?}
?? ?//運行結(jié)果
?? ?//12properties文件的寫入
因為properties文件數(shù)據(jù)是以集合形式存儲,所以可以用put()方法將KV鍵值對存入Properties集合中,再通過輸出流使用store()方法將集合中的臨時數(shù)據(jù),持久化寫入硬盤中存儲。
//properties格式文件的寫入
?? ?try{
?? ??? ?Properties props=new Properties();
?? ??? ?props.put("Q", "123");
?? ??? ?props.put("W", "432");
?? ??? ?props.put("E", "986");
?? ??? ?props.put("R", "457");
?? ?//使用“輸出流”,將properties集合中的KV鍵值對,寫入*.properties文件
?? ? ?try (BufferedOutputStream bos = new BufferedOutputStream(
?? ??? ??? ?new FileOutputStream("D:\\student\\bbb.properties"))) {
?? ??? ?props.store(bos, "just do IT");
?? ? ?}
?? ?} catch (IOException e) {
?? ??? ?e.printStackTrace();
?? ?}
//運行結(jié)果
//?? ?#just do IT
//?? ?#Wed Jun 01 00:14:57 CST 2022
//?? ?W=432
//?? ?R=457
//?? ?Q=123
//?? ?E=986總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
ruoyi-springboot框架新增模塊調(diào)接口報404的解決方案
這篇文章主要介紹了ruoyi-springboot框架新增模塊調(diào)接口報404的解決方案,文中通過代碼示例給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-03-03
spring-@Autowired注入與構(gòu)造函數(shù)注入使用方式
這篇文章主要介紹了spring-@Autowired注入與構(gòu)造函數(shù)注入使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12

