Android中Properties使用

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   java   使用   os   

一、概述      

      properties檔案是一種設定檔,主要用於表達配置資訊,檔案類型為*.properties,格式為文字檔,檔案的內容是格式是"鍵=值"的格式,在properties
檔案中,可以用"#"來作注釋。

  例如:設定檔bruts.properties裡面的內容為:

  #get
  #time 2014.08.04

  login=false
  ModuleNames=\u65B0\u95FB,\u70ED\u70B9,\u56FE\u7247,\u7B80\u8BAF,\u8BBE\u7F6E
  register=false
  ModuleIdsIds=14,16,17
  apkId=1
  bussesid=439

二、內容 

     Properties類的重要方法

     Properties 類存在於包 Java.util 中,該類繼承自 Hashtable

  1. getProperty ( String  key) ,   用指定的鍵在此屬性列表中搜尋屬性。也就是通過參數 key ,得到 key 所對應的 value。
  2. load ( InputStream  inStream) ,從輸入資料流中讀取屬性列表(鍵和元素對)。通過對指定的檔案(比如說上面的 test.properties 檔案)進行裝載來擷取該文
        件中的所有鍵 - 值對。以供 getProperty ( String  key) 來搜尋。
  3. setProperty ( String  key, String  value) ,調用 Hashtable 的方法 put 。他通過調用基類的put方法來設定 鍵 - 值對。 
  4. store ( OutputStream  out, String  comments) ,   以適合使用 load 方法載入到 Properties 表中的格式,將此 Properties 表中的屬性列表(鍵和元素
        對)寫入輸出資料流。與 load 方法相反,該方法將鍵 - 值對寫入到指定的檔案中去。
  5. clear () ,清除所有裝載的 鍵 - 值對。該方法在基類中提供。

 

三、理解 

  通過一段代碼來理解該類的使用,讀取bruts.properties檔案中的內容。

 1 package com.example.androidprojecttest; 2  3 import java.io.InputStream; 4 import java.util.Properties; 5  6 import android.os.Bundle; 7 import android.os.Handler; 8 import android.os.Message; 9 import android.os.Messenger;10 import android.R.id;11 import android.R.integer;12 import android.R.string;13 import android.app.Activity;14 import android.content.Intent;15 import android.view.Menu;16 import android.view.View;17 import android.view.View.OnClickListener;18 import android.widget.Button;19 import android.widget.LinearLayout;20 import android.widget.TextView;21 22 public class MainActivity extends Activity {23 24     private TextView tView;25 26     private String age="";27     private String name="";28     private String sex="";29     @Override30     protected void onCreate(Bundle savedInstanceState) {31         super.onCreate(savedInstanceState);32         setContentView(R.layout.activity_main);33         tView = (TextView) findViewById(R.id.tv);34         35         try {36             Properties pro = new Properties();37             InputStream is = this.getAssets().open("bruts.properties");38             pro.load(is);39             name=(String)pro.get("name");40             age=(String)pro.get("age");41             sex=(String)pro.get("sex");42             43             is.close();44         } catch (Exception e) {45             e.printStackTrace();46         }47         48         tView.setText("name: "+name+"\nage: "+age+"\nsex: "+sex);49     }50 51 }

  

  bruts.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.