Android under the Properties save program configuration

Source: Internet
Author: User

Read and write functions such as the following:

<textarea cols="50" rows="15" name="code" class="java">import java.io.fileinputstream;import java.io.fileoutputstream;import java.util.properties;public Properties Loadconfig (context context, String file) {Properties Properties = new Properties (); try {fileinputstream s = new fileinputs Tream (file);p roperties.load (s);} catch (Exception e) {e.printstacktrace ();} return properties;} public void Saveconfig (context context, String file, properties properties) {try {fileoutputstream s = new Fileoutputstrea m (file, False);p Roperties.store (S, "");} catch (Exception e) {e.printstacktrace ();}}</textarea>
Orz, did you find anything? Yes, these two functions have nothing to do with Android.
So they can be used in other standard Java programs as well.
In Android, it is better to read and write from a pure string, parse it yourself, or use XML to save the configuration.
Properties appear simpler and more intuitive, due to the need for a lot of code, and XML operation is far less convenient than the properties

Usage such as the following:
Write configuration:
<textarea cols="50" rows="15" name="code" class="java">Properties prop = new properties ();p rop.put ("Prop1", "abc");p rop.put ("Prop2", 1);p rop.put ("Prop3", 3.14); Saveconfig (This, "/sdcard/config.dat", prop);</textarea>
Read configuration:
<textarea cols="50" rows="15" name="code" class="java">Properties prop = Loadconfig (this, "/sdcard/config.dat"); String Prop1 = Prop.get ("Prop1");</textarea>
Note: You can also use the context Openfileinput and Openfileoutput methods to read and write files
At this point the file will be saved under/data/data/package_name/files and managed by the system
When you read and write to a file using this method, you cannot specify a detailed path for the file.

Android under the Properties save program configuration

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.