The properties used in Android

Source: Internet
Author: User

I. Overview

The properties file is a configuration file that is used primarily to express configuration information with a file type of *. properties, formatted as text files, the contents of the file is formatted as "key = value" in the format, in the properties
File, you can use "#" to make comments.

For example: The contents of the configuration file bruts.properties are:

#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

Second, the content

Important methods of the properties class

The Properties class exists in package Java.util, which inherits from Hashtable

1. GetProperty (string  key),   searches for properties in this property list with the specified key. That is, by the parameter key, the value corresponding to the key is obtained.
2. Load (inputstream  instream) to read the list of attributes (key and element pairs) from the input stream. Through the specified file (for example, the above test. properties  file) to obtain all key-value pairs in the
      pieces of this article. To search for GetProperty (string  key).
3. SetProperty (string  key, string  value), calls the method of Hashtable put. He sets a key-value pair by calling the put method of the base class.  
4. Store (outputstream  out, string  comments),   to be loaded into         pairs) in the Hilite1 >properties  table to the output stream. In contrast to the load method, the method writes a key-value pair to the specified file.
5. Clear () Clears all loaded key-value pairs. This method is provided in the base class.

Third, understanding

using a piece of code to understand the use of the class, reads the contents of the Bruts.properties file.

1  Packagecom.example.androidprojecttest;2 3 ImportJava.io.InputStream;4 Importjava.util.Properties;5 6 ImportAndroid.os.Bundle;7 ImportAndroid.os.Handler;8 ImportAndroid.os.Message;9 ImportAndroid.os.Messenger;Ten ImportAndroid. R.id; One ImportAndroid. R.integer; A ImportAndroid. r.string; - Importandroid.app.Activity; - Importandroid.content.Intent; the ImportAndroid.view.Menu; - ImportAndroid.view.View; - ImportAndroid.view.View.OnClickListener; - ImportAndroid.widget.Button; + Importandroid.widget.LinearLayout; - ImportAndroid.widget.TextView; +  A  Public classMainactivityextendsActivity { at  -     PrivateTextView TView; -  -     PrivateString age= ""; -     PrivateString name= ""; -     PrivateString sex= ""; in @Override -     protected voidonCreate (Bundle savedinstancestate) { to         Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); -TView =(TextView) Findviewbyid (r.id.tv); the          *         Try { $Properties Pro =NewProperties ();Panax NotoginsengInputStream is = This. Getassets (). Open ("Bruts.properties"); - Pro.load (IS); theName= (String) pro.get ("name"); +Age= (String) pro.get ("Age"); Asex= (String) pro.get ("Sex"); the              + is.close (); -}Catch(Exception e) { $ e.printstacktrace (); $         } -          -Tview.settext ("Name:" +name+ "\nage:" +age+ "\nsex:" +sex); the     } - Wuyi}

  

Content in the Bruts.properties file:

The effect is:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.