本文實例講述了java讀取properties配置文件的方法。分享給大家供大家參考。具體分析如下:
這兩天做java項目,用到屬性文件,到網上查資料,好半天也沒有找到一個滿意的方法能讓我讀取到.properties文件中屬性值,很是郁悶,網上講的獲取屬性值大概有以下方法,以下三種方法逐漸優化,以達到最好的效果以下都以date.properties文件為例,該文件放在src目錄下,文件內容為:
startdate=2011-02-07
totalweek=25
方法一:
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
|
public class Stweek { static private String startdate = null ; static private String totalweek = null ; synchronized static public void loads(){ if (startdate == null || totalweek == null ) { FileInputStream is = null ; Properties dbProps = new Properties(); try { is = new FileInputStream(filepath); dbProps.load(is); startdate = dbProps.getProperty( "startdate" ); totalweek = dbProps.getProperty( "totalweek" ); } catch (Exception e) { System.err.println( "不能讀取屬性文件. " + "請確保db.properties在CLASSPATH指定的路徑中" ); } } } public static String getStartdate() { if (tartdate== null ) loads(); return startdate; } public static String getTotalweek() { if (startdate== null ) loads(); return totalweek; } } |
以上方法雖然也能獲得配置文件內容,可是其最大的問題就是文件路徑的定位(就是代碼中的filepath取值問題),當采用絕對定位的時候,如果將工程移到另外一個盤符下運行,就需要修改源代碼,否則就會報錯,可是如果使用相對路徑,當Stweek 類移到另外一個包中時,還是要修改源代碼,否則會報錯,所以說這個方法局限太大,不好,以下方法二能解決這個問題,可是其還是有些問題
方法二:
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
|
public class Stweek { InputStream is = null ; Properties dbProps = null ; public Stweek() { // TODO Auto-generated constructor stub is = getClass().getResourceAsStream( "/date.properties" ); dbProps = new Properties(); try { dbProps.load(is); } catch (Exception e) { System.err.println( "不能讀取屬性文件. " + "請確保db.properties在CLASSPATH指定的路徑中" ); } } public String getStartdate() { String sd = null ; sd = dbProps.getProperty( "startdate" ); return sd; } public String getTotalweek() { String totalweek= null ; totalweek = dbProps.getProperty( "totalweek" ); return totalweek; } } |
這個方法的好處就是不用指出配置文件的絕對路徑,而且不管是將Stweek 類放到另外的包中,還是將整個工程移到到另外的盤符下,代碼依然可以正常運行,不會有找不到文件的問題,可是這個方法仍然有一個重大的缺陷,因為我們往往希望配置文件能夠緩存在內存中,這樣不用每次讀取時都要訪問硬盤(訪問外存太浪費時間),為此我們希望使用靜態變量、靜態方法來緩存和獲得變量,同時能夠實現這些值的動態加載(load),那么問題來了,因為getClass().getResourceAsStream("/date.properties"); 這一句只能出現在構造函數中(同鞋可以自己測試一下),動態load中并不能使用這個方法,怎么辦呢,且看第三個方法
方法三:
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
|
import java.io.InputStream; import java.util.Properties; public class Stweek { static private String startdate = null ; static private String totalweek = null ; static { loads(); } synchronized static public void loads(){ if (startdate == null || totalweek == null ) { InputStream is = Stweek. class .getResourceAsStream( "/date.properties" ); Properties dbProps = new Properties(); try { dbProps.load(is); startdate = dbProps.getProperty( "startdate" ); totalweek = dbProps.getProperty( "totalweek" ); } catch (Exception e) { System.err.println( "不能讀取屬性文件. " + "請確保db.properties在CLASSPATH指定的路徑中" ); } } } public static String getStartdate() { if (startdate== null ) loads(); return startdate; } public static String getTotalweek() { if (startdate== null ) loads(); return totalweek; } } |
這個方法不僅能夠緩存配置文件內容,還能夠做到自動加載配置文件的內容到內存,使用者完全不用考慮手動加載的過程,只需要在需要用到的地方直接調用Stweek.getStartdate()即可(因為是靜態方法,事先連對像也不用創建的),這樣如果內存中有緩存,函數就會直接讀取內存中的數據,節省時間,如果沒有緩存也不用擔心,系統會自動為你加載,使用者完全不用知道其是如何實現的,只需要知道我能直接調用函數獲得想要的值就行了,呵呵,簡單吧
備注:(與上文無關,自己的測試)
希望本文所述對大家的java程序設計有所幫助。