幾種讀取屬性檔案的JAVA實現方式

來源:互聯網
上載者:User

 1.使用java.util.Properties類的load()方法
  
  樣本:

Java代碼
  1. InputStream in = lnew BufferedInputStream(new FileInputStream(name));   
  2.   Properties p = new Properties();   
  3.   p.load(in);  
InputStream in = lnew BufferedInputStream(new FileInputStream(name));  Properties p = new Properties();  p.load(in);

  
  2.使用java.util.ResourceBundle類的getBundle()方法  
  樣本:

Java代碼
  1. ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());  
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

  
  3.使用java.util.PropertyResourceBundle類的建構函式  
  樣本:

Java代碼
  1. InputStream in = new BufferedInputStream(new FileInputStream(name));   
  2.   ResourceBundle rb = new PropertyResourceBundle(in);  
InputStream in = new BufferedInputStream(new FileInputStream(name));  ResourceBundle rb = new PropertyResourceBundle(in);

  
  4.使用class變數的getResourceAsStream()方法  
  樣本:

Java代碼
  1. InputStream in = JProperties.class.getResourceAsStream(name);   
  2.   Properties p = new Properties();   
  3.   p.load(in);  
InputStream in = JProperties.class.getResourceAsStream(name);  Properties p = new Properties();  p.load(in);

  
  5.使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法  
  樣本:

Java代碼
  1. InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);   
  2.   Properties p = new Properties();   
  3.   p.load(in);  
InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);  Properties p = new Properties();  p.load(in);

  
  6.使用java.lang.ClassLoader類的getSystemResourceAsStream()靜態方法  
  樣本:

Java代碼
  1. InputStream in = ClassLoader.getSystemResourceAsStream(name);   
  2.   Properties p = new Properties();   
  3.   p.load(in);  
InputStream in = ClassLoader.getSystemResourceAsStream(name);  Properties p = new Properties();  p.load(in);

  
  補充
  
  Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
  
  樣本:

Java代碼
  1. InputStream in = context.getResourceAsStream(path);   
  2.   Properties p = new Properties();   
  3.   p.load(in);   
InputStream in = context.getResourceAsStream(path);  Properties p = new Properties();  p.load(in); 

        其中name為properties檔案名稱字.但我在網上發現有人說要寫properties檔案的絕對路徑,否則測試   不 能通過.我沒驗證過,有興趣的朋友可以試試.
      就我個人而言我是比較偏向用第3方法.我在網上找到一篇介紹的更為詳細的文章,全文如下:
  在設計時,我們往往需要訪問一些適合本地修改的配置資訊,如果作為靜態變數,那麼每次修改都需要重新編譯一個class,.config儲存此類資訊並不適合,這時我們需要ResourceBundle。
   通過ResourceBundle,我們需要訪問位於/WEB-INF/classes目錄下的一個尾碼名為properties的文本類型檔案,從裡面讀取我們需要的值。

   

Java代碼
  1. Locale locale = Locale.getDefault();   
  2.     ResourceBundle localResource = ResourceBundle.getBundle("ConnResource", locale);   
  3.     
  4.     String value = localResource.getString("test");   
  5.     System.out.println("ResourceBundle: " + value);  
Locale locale = Locale.getDefault();    ResourceBundle localResource = ResourceBundle.getBundle("ConnResource", locale);     String value = localResource.getString("test");    System.out.println("ResourceBundle: " + value);

    這裡對應了/WEB-INF/class/ConnResource.properties檔案內容為:

    test=hello world
    列印出來的結果就是hello world  
    請注意,這裡我們可以利用Locale和ResourceBundle的這個組合建立國際化的java程式。我們可以把locale執行個體化為

Java代碼
  1. new Locale("zh","CN");  
new Locale("zh","CN");

    通過

Java代碼
  1. ResourceBundle.getBundle("MessagesBundle",  locale);  
ResourceBundle.getBundle("MessagesBundle",  locale);

    系統將自動尋找MessagesBundle_zh_CN,即定義為中國大陸地區簡體中文。如果沒有該檔案,則會依次尋找MessagesBundle_zh,MessagesBundle,直到找到為止。

看見沒有?用這種方法還可以進行國際化的處理.應該功能上比較強大點.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.