This time can and see very uncomfortable sharedpreferences say goodbye. The usage is disgusting. Save the fart big point data also uses commit.
The end of the groove, on the code
Localstorage.java
Import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.outputstream;import Java.io.unsupportedencodingexception;import Java.net.urldecoder;import Java.net.urlencoder;import Java.util.hashmap;import Java.util.map;import Java.util.properties;import Java.util.set;import android.content.context;/** * Android local storage, primarily used to store simple key value pairs. Provide methods for adding, deleting, modifying and checking. Customizable path * * @author Administrator * */public class Localstorage {private static properties Properties = New Properties ();p Rivate static String filepath;private localstorage () {}/** * * @param CTX * @param filename * file name * @return */ public static localstorage Get (Context ctx, String fileName) {return get (Ctx.getcachedir () + "/" + FileName);} /** * * @param filePath * File absolute path * @return */public static localstorage get (String filePath) {createFile (Filep ATH); filepath = filepath;try {properties.load (new FileInputStream (Filepath)); return new Localstorage ();} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} return null;} private static void CreateFile (String fileName) {File File = new file (fileName); if (!file.exists ()) {String path = File.ge Tabsolutepath (); string[] Sourcestrarray = Path.split ("/"); String Dirpath = ""; for (int i = 0; i < sourcestrarray.length-1; i++) {if (!sourcestrarray[i].equals (")) {Dirpath + ="/"+ Sourcestrarray[i];}} New File (Dirpath). Mkdirs (); try {file.createnewfile ();} catch (IOException e) {e.printstacktrace ();}}} Public String getasstring (string key) {if (key = = null) {return null;} Properties Props = new properties (); try {props.load (new FileInputStream (filepath)); String value = Props.getproperty (key); value = Urldecoder.decode (value, "Utf-8"); return value;} catch (Exception e) {e.printstacktrace (); return null;}} public int Getasint (string key) {string str = getasstring (key), if (str = = null) Return-9999;return integer.valueof (str). InTValue ();} public boolean Getasboolean (string key) {string str = getasstring (key); if (str = = null) return false;if (Str.equals ("true") ) return True;return false;} Public long Getaslong (string key) {string str = getasstring (key); if (str = = null) Return-9999;return long.valueof (str). Lon Gvalue ();} Public float Getasfloat (string key) {string str = getasstring (key); if (str = = null) Return-9999;return float.valueof (str). Floatvalue ();} Public double getasdouble (string key) {string str = getasstring (key); if (str = = null) Return-9999;return double.valueof (St R). Doublevalue ();} /** * Add * * @param keyname * @param keyvalue */public void put (string keyname, Object keyvalue) {//handle Chinese garbled string value = Keyvalue.tostring (); try {value = Urlencoder.encode (value, "Utf-8");} catch (Unsupportedencodingexception E1) { E1.printstacktrace ();} try {outputstream fos = new FileOutputStream (filepath);p roperties.setproperty (KeyName, value);p roperties.store (FOS, NULL);} catch (IOException e) {}}/** * update * * @param keyname* @param keyvalue */public void Update (String keyname, String keyvalue) {try {properties.load (new FileInputStream (Filepat h)); OutputStream fos = new FileOutputStream (filepath);p roperties.setproperty (KeyName, KeyValue);p Roperties.store ( FOS, null);} catch (IOException e) {}}/** * deleted from key * * @param key */public void Delete (String key) {try {FileInputStream FIS = new Fi Leinputstream (filepath);p roperties.load (FIS); map<string, string> map = new hashmap<string, string> (); set<object> KeySet = Properties.keyset (); for (Object object:keyset) {String objectkey = (string) Object; String value = (string) properties.get (Objectkey); Map.put (Objectkey, value);} Map.Remove (Key);p roperties.remove (key); for (java.util.map.entry<string, string> entry:map.entrySet ()) { Properties.setproperty (Entry.getkey (), Entry.getvalue ());} FileOutputStream fos = new FileOutputStream (filepath);p roperties.store (fos, "Delete key:" + key); Fos.close (); fis.close ();} catch (Exception e) {E.printstacktracE ();}}}
How to use
This gets the Localstorage object. The parameter can also be the absolute path of the file, or it can pass directly to the context and file name. Localstorage localstorage = Localstorage.get ("test.txt");//Add Localstorage.put ("key", "haha");//Update localstorage.update ("Key", "value");//lookup, which provides multiple getas methods. To find the data type localstorage.getasstring ("key");//delete Localstorage.delete ("key");
Android Ultra Lightweight data storage class