Android ultra-lightweight data storage

Source: Internet
Author: User

Android ultra-lightweight data storage

This time, I can say goodbye to sharedpreferences. The method is disgusting. Commit is also used to store fart big data.

 

The spam is over and the code is displayed.

 

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 is mainly used to store simple key-value pairs. Provides addition, deletion, modification, and query methods. You can customize the path ** @ author Administrator **/public class LocalStorage {private static Properties properties Properties = new Properties (); private 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 * absolute file path * @ return */public static LocalStorage get (String filePath) {createFile (filePath); 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. getAbsolutePath (); 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 ). longValue ();} 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 (str ). doubleValue ();}/*** add ** @ param keyname * @ param keyvalue */public void put (String keyname, Object keyvalue) {// process Chinese garbled String value = keyvalue. toString (); try {value = URLEncoder. encode (value, "UTF-8");} catch (UnsupportedEncodingException e1) {e1.printStackTrace () ;}try {OutputStream fos = new FileOutputStream (filepath); properties. setProperty (keyname, value); properties. store (fos, null);} catch (IOException e) {}}/*** update ** @ param keyname * @ param keyvalue */public void update (String keyname, string keyvalue) {try {properties. load (new FileInputStream (filepath); OutputStream fos = new FileOutputStream (filepath); properties. setProperty (keyname, keyvalue); properties. store (fos, null);} catch (IOException e) {}}/*** delete ** @ param key */public void delete (String key) based on the key) {try {FileInputStream FCM = new FileInputStream (filepath); properties. load (FCM); Map
 
  
Map = new HashMap
  
   
(); Set
   KeySet = properties. keySet (); for (Object object: keySet) {String objectkey = (String) object; String value = (String) properties. get (objectkey); map. put (objectkey, value);} map. remove (key); properties. remove (key); for (java. util. map. entry
    
     
Entry: map. entrySet () {properties. setProperty (entry. getKey (), entry. getValue ();} FileOutputStream fos = new FileOutputStream (filepath); properties. store (fos, "delete key:" + key); fos. close (); FCM. close ();} catch (Exception e) {e. printStackTrace ();}}}
    
  
 


 

Usage

 

 

// Obtain the LocalStorage object. The parameter can also be an absolute path of the file, or you can directly input Context and file name. LocalStorage localStorage = LocalStorage. get ("test.txt"); // Add localStorage. put ("key", "Haha"); // update localStorage. update ("key", "value"); // search. Multiple getAs methods are provided here. Obtain the data and find the corresponding data type localStorage. getAsString ("key"); // delete localStorage. delete ("key ");


 

 

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.