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 ");