java中properties類的操作實例詳解
知識學而不用,就等于沒用,到真正用到的時候還得重新再學。最近在看幾款開源模擬器的源碼,里面涉及到了很多關于properties類的引用,由于java已經好久沒用了,而這些模擬器大多用java來寫,外加一些腳本語言python,perl之類的,不得已,又得重新拾起。本文通過看《java編程思想》和一些網友的博客總結而來,只為簡單介紹properties類的相關操作。
一、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
2
3
4
5
6
7
8
|
import java.util.properties; public class readjvm { public static void main(string[] args) { properties pps = system.getproperties(); pps.list(system.out); } } |
結果:
2、隨便新建一個配置文件(test.properties)
1
2
3
|
name=jj weight= 4444 height= 3333 |
1
2
3
4
5
6
7
8
9
10
11
12
|
public class getproperties { public static void main(string[] args) throws filenotfoundexception, ioexception { properties pps = new properties(); pps.load( new fileinputstream( "test.properties" )); enumeration enum1 = pps.propertynames(); //得到配置文件的名字 while (enum1.hasmoreelements()) { string strkey = (string) enum1.nextelement(); string strvalue = pps.getproperty(strkey); system.out.println(strkey + "=" + strvalue); } } } |
3、一個比較綜合的實例
根據key讀取value
讀取properties的全部信息
寫入新的properties信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
//關于properties類常用的操作 public class testproperties { //根據key讀取value public static string getvaluebykey(string filepath, string key) { properties pps = new properties(); try { inputstream in = new bufferedinputstream ( new fileinputstream(filepath)); pps.load(in); string value = pps.getproperty(key); system.out.println(key + " = " + value); return value; } catch (ioexception e) { e.printstacktrace(); return null ; } } //讀取properties的全部信息 public static void getallproperties(string filepath) throws ioexception { properties pps = new properties(); inputstream in = new bufferedinputstream( new fileinputstream(filepath)); pps.load(in); enumeration en = pps.propertynames(); //得到配置文件的名字 while (en.hasmoreelements()) { string strkey = (string) en.nextelement(); string strvalue = pps.getproperty(strkey); system.out.println(strkey + "=" + strvalue); } } //寫入properties信息 public static void writeproperties (string filepath, string pkey, string pvalue) throws ioexception { properties pps = new properties(); inputstream in = new fileinputstream(filepath); //從輸入流中讀取屬性列表(鍵和元素對) pps.load(in); //調用 hashtable 的方法 put。使用 getproperty 方法提供并行性。 //強制要求為屬性的鍵和值使用字符串。返回值是 hashtable 調用 put 的結果。 outputstream out = new fileoutputstream(filepath); pps.setproperty(pkey, pvalue); //以適合使用 load 方法加載到 properties 表中的格式, //將此 properties 表中的屬性列表(鍵和元素對)寫入輸出流 pps.store(out, "update " + pkey + " name" ); } public static void main(string [] args) throws ioexception{ //string value = getvaluebykey("test.properties", "name"); //system.out.println(value); //getallproperties("test.properties"); writeproperties( "test.properties" , "long" , "212" ); } } |
結果:
test.properties中文件的數據為:
1
2
3
4
5
6
|
#update long name #sun feb 23 18 : 17 : 16 cst 2014 name=jj weight= 4444 long = 212 height= 3333 |
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://www.cnblogs.com/bakari/p/3562244.html