日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java Properties的使用

發布時間:2023/12/20 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java Properties的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉自:https://www.cnblogs.com/bakari/p/3562244.html

?

一、Java Properties類

??? Java中有個比較重要的類Properties(Java.util.Properties),主要用于讀取Java的配置文件,各種語言都有自己所支持的配置文件,配置文件中很多變量是經常改變的,這樣做也是為了方便用戶,讓用戶能夠脫離程序本身去修改相關的變量設置。像Python支持的配置文件是.ini文件,同樣,它也有自己讀取配置文件的類ConfigParse,方便程序員或用戶通過該類的方法來修改.ini配置文件。在Java中,其配置文件常為.properties文件,格式為文本文件,文件的內容的格式是“鍵=值”的格式,文本注釋信息可以用"#"來注釋。

Properties類繼承自Hashtable,如下:

它提供了幾個主要的方法:

1. getProperty ( String key),用指定的鍵在此屬性列表中搜索屬性。也就是通過參數 key ,得到 key 所對應的 value。

2. load ( InputStream inStream),從輸入流中讀取屬性列表(鍵和元素對)。通過對指定的文件(比如說上面的 test.properties 文件)進行裝載來獲取該文件中的所有鍵 - 值對。以供 getProperty ( String key) 來搜索。

3. setProperty ( String key, String value) ,調用 Hashtable 的方法 put 。他通過調用基類的put方法來設置 鍵 - 值對。

4. store ( OutputStream out, String comments),以適合使用 load 方法加載到 Properties 表中的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流。與 load 方法相反,該方法將鍵 - 值對寫入到指定的文件中去。

5. clear (),清除所有裝載的 鍵 - 值對。該方法在基類中提供。

?

二、Java讀取Properties文件

??? Java讀取Properties文件的方法有很多,詳見:?Java讀取Properties文件的六種方法

但是最常用的還是通過java.lang.Class類的getResourceAsStream(String name)方法來實現,如下可以這樣調用:

InputStream in = getClass().getResourceAsStream("資源Name");作為我們寫程序的,用此一種足夠。

或者下面這種也常用:

InputStream in = new BufferedInputStream(new FileInputStream(filepath));

?

三、相關實例

下面列舉幾個實例,加深對Properties類的理解和記憶。

我們知道,Java虛擬機(JVM)有自己的系統配置文件(system.properties),我們可以通過下面的方式來獲取。

1、獲取JVM的系統屬性

1 import java.util.Properties; 2 3 public class ReadJVM { 4 public static void main(String[] args) { 5 Properties pps = System.getProperties(); 6 pps.list(System.out); 7 } 8 }

結果:

?

2、隨便新建一個配置文件(Test.properties)

name=JJ Weight=4444 Height=3333 1 public class getProperties {2 public static void main(String[] args) throws FileNotFoundException, IOException {3 Properties pps = new Properties();4 pps.load(new FileInputStream("Test.properties"));5 Enumeration enum1 = pps.propertyNames();//得到配置文件的名字6 while(enum1.hasMoreElements()) {7 String strKey = (String) enum1.nextElement();8 String strValue = pps.getProperty(strKey);9 System.out.println(strKey + "=" + strValue); 10 } 11 } 12 }

3、一個比較綜合的實例

根據key讀取value

讀取properties的全部信息

寫入新的properties信息

1 //關于Properties類常用的操作2 public class TestProperties {3 //根據Key讀取Value4 public static String GetValueByKey(String filePath, String key) {5 Properties pps = new Properties();6 try {7 InputStream in = new BufferedInputStream (new FileInputStream(filePath)); 8 pps.load(in);9 String value = pps.getProperty(key); 10 System.out.println(key + " = " + value); 11 return value; 12 13 }catch (IOException e) { 14 e.printStackTrace(); 15 return null; 16 } 17 } 18 19 //讀取Properties的全部信息 20 public static void GetAllProperties(String filePath) throws IOException { 21 Properties pps = new Properties(); 22 InputStream in = new BufferedInputStream(new FileInputStream(filePath)); 23 pps.load(in); 24 Enumeration en = pps.propertyNames(); //得到配置文件的名字 25 26 while(en.hasMoreElements()) { 27 String strKey = (String) en.nextElement(); 28 String strValue = pps.getProperty(strKey); 29 System.out.println(strKey + "=" + strValue); 30 } 31 32 } 33 34 //寫入Properties信息 35 public static void WriteProperties (String filePath, String pKey, String pValue) throws IOException { 36 Properties pps = new Properties(); 37 38 InputStream in = new FileInputStream(filePath); 39 //從輸入流中讀取屬性列表(鍵和元素對) 40 pps.load(in); 41 //調用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。 42 //強制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調用 put 的結果。 43 OutputStream out = new FileOutputStream(filePath); 44 pps.setProperty(pKey, pValue); 45 //以適合使用 load 方法加載到 Properties 表中的格式, 46 //將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流 47 pps.store(out, "Update " + pKey + " name"); 48 } 49 50 public static void main(String [] args) throws IOException{ 51 //String value = GetValueByKey("Test.properties", "name"); 52 //System.out.println(value); 53 //GetAllProperties("Test.properties"); 54 WriteProperties("Test.properties","long", "212"); 55 } 56 }

結果: Test.properties中文件的數據為:

#Update long name #Sun Feb 23 18:17:16 CST 2014 name=JJ Weight=4444 long=212 Height=3333

轉載于:https://www.cnblogs.com/jack-L/p/10444577.html

總結

以上是生活随笔為你收集整理的Java Properties的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。