java讀取properties設定檔

來源:互聯網
上載者:User

標籤:最佳化   import   微軟   ons   except   track   解決問題   內容   size   

java讀取.properties設定檔

       這兩天做java項目,用到屬性檔案,到網上查資料,好半天也沒有找到一個愜意的方法能讓我讀取到.properties檔案裡屬性值。非常是鬱悶,網上講的擷取屬性值大概有下面方法,下面三種方法逐漸最佳化,以達到最好的效果下面都以date.properties檔案為例。該檔案放在src檔案夾下。檔案內容為

startdate=2011-02-07
totalweek=25

方法一:

 

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取值問題)。當採用絕對位置的時候,假設將project移到另外一個盤符下執行。就須要改動源碼,否則就會報錯。但是假設使用相對路徑,當Stweek類移到另外一個包中時,還是要改動源碼,否則會報錯。所以說這種方法局限太大,不好。下面方法二能解決問題,但是其還是有些問題

方法二:

 

public class Stweek{  InputStream is = null;Properties dbProps = null;public Stweek(){// TODO Auto-generated constructor stubis = 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 類放到另外的包中。還是將整個project移到到另外的盤符下,代碼依舊可以正常執行,不會有找不到檔案的問題。但是這種方法仍然有一個重大的缺陷。由於我們往往希望設定檔可以緩衝在記憶體中。這樣不用每次讀取時都要訪問硬碟(訪問外存太浪費時間),為此我們希望使用靜態變數、靜態方法來緩衝和獲得變數,同一時候可以實現這些值的動態載入(load),那麼問題來了,由於getClass().getResourceAsStream("/date.properties"); 這一句僅僅能出如今建構函式中(同鞋能夠自己測試一下),動態load中並不能使用這種方法,怎麼辦呢,且看第三個方法

方法三:

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讀取properties設定檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.