Android 讀取Properties設定檔的小例子_Android

來源:互聯網
上載者:User
開發應用的時候會有一些有可能會變得值,例如webservice地址 應用的一些ID等等,之前一直都是直接在應用中改代碼,不是忘點這忘點那,於是想到了可以用Properties設定檔,我把網址等變數配置的設定檔中,這樣之後再改的話就直接改設定檔就行了,就不用改代碼了
下面給大家說說Properties的用法
複製代碼 代碼如下:

 public static String getPropertiesURL(Context c, String s) {
  String url = null;
  Properties properties = new Properties();
  try {
   properties.load(c.getAssets().open("property.properties"));
   url = properties.getProperty(s);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return url;
 }

這裡我把設定檔放在了assets檔案夾下 大家也可以放在raw下 還可以跟我們Java檔案同級這樣我們就可以讀到Properties值了
設定檔的寫法很簡單:
例如 url=www.jb51.net就行注釋用#號

聯繫我們

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