android中設定檔property的用途以及使用

來源:互聯網
上載者:User

 1.首先在原始碼根目錄(src下)下建立一個名為netconfig.properties的檔案(也可以在其他目錄下)。

2.開啟netconfig.properties檔案,在該檔案中添加下列代碼.

Java代碼:

#該處為注釋   IP=192.168.1.111  PORT=3535    MSG=這是一個很長的字串,<br>我們可以使其換行 \   一行顯示不下時,<br>可以分行顯示。  #該處為注釋IP=192.168.1.111PORT=3535MSG=這是一個很長的字串,<br>我們可以使其換行 \一行顯示不下時,<br>可以分行顯示。 

說明:上面代碼中,#為設定檔中的注釋,而在設定檔中,可以使用<br>等進行格式處理,在設定檔中,如果某個屬性過長,一行不能輸入完全是,可以通過 \ 通知系統,下一行同樣為該屬性的值。
3.之後建立一個工具類Utils類,其代碼如下所列。

**   *  @description: 工具類   *  @version 1.0   *  @created on 2010-12-3 下午02:39:52   */  public class Utils {       /**       * 得到netconfig.properties設定檔中的所有配置屬性       *        * @return Properties對象       */      public static Properties getNetConfigProperties() {           Properties props = new Properties();           InputStream in = Utils.class.getResourceAsStream("/netconfig.properties");           try {               props.load(in);           } catch (IOException e) {               e.printStackTrace();           }           return props;       }   }  

4.在需要使用設定檔中配置的屬性值時,直接調用上述方法即可,如下所示。

String IP = Utils.getNetConfigProperties().getProperty("IP");  

 

相關文章

聯繫我們

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