Android saves the key value, instead of occupying the memory sharedPreference, but the local Properties file.

Source: Internet
Author: User

SharedPreference is placed under data/package name.
Memory usage. If a large amount of data is stored, it needs to be stored in the sdcard. Therefore, SharedPreferences is inconvenient. It is better to use the Properties class directly.
The file can be passed in as a string and the correct value can be obtained through access!
[Java]
Package com. nil. cache;
 
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java. util. Properties;
 
Public class PropertiesConfig extends Properties {
 
String propertyPath = "";
Private PropertiesConfig (String path ){
PropertyPath = path;
};
 
Public static PropertiesConfig getInstance (String path ){
{
File file = new File (path );
If (file. exists ()){
Try {
File. createNewFile ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
PropertiesConfig pro = new PropertiesConfig (path );
Try {
InputStream is = new FileInputStream (file );
Pro. load (is );
Is. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
Return pro;
}

}
 
@ Override
Public Object setProperty (String key, String value ){
Super. setProperty (key, value );
Try {
This. store (new FileOutputStream (this. propertyPath ),
"UTF-8 ");
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
Return value;
}
 
Public Object put (String key, String value ){
Super. put (key, value );
Try {
This. store (new FileOutputStream (this. propertyPath ),
"UTF-8 ");
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
Return value;
}
}
Hey! How to use it?
You can call it as follows:
[Java]
PropertiesConfig. getInstance ("/sdcard/client_config.xml"). setProperty ("187", "name ");

PropertiesConfig. getInstance ("/sdcard/client_config.xml"). get ("187 ");

 

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.