中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

android 超輕量級(jí)數(shù)據(jù)存儲(chǔ)類

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
    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本地存儲(chǔ) ,主要用于存儲(chǔ)簡單key value鍵值對(duì)。提供增、刪、改、查方法。 可自定義路徑 
     *  
     * @author Administrator 
     * 
     */  
    public class LocalStorage {  
        private static Properties properties = new Properties();  
        private static String filepath;  
      
        private LocalStorage() {  
        }  
      
        /** 
         *  
         * @param ctx 
         * @param fileName 
         *            文件名 
         * @return 
         */  
        public static LocalStorage get(Context ctx, String fileName) {  
            return get(ctx.getCacheDir() + "/" + fileName);  
        }  
      
        /** 
         *  
         * @param filePath 
         *            文件絕對(duì)路徑 
         * @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();  
        }  
      
        /** 
         * 添加 
         *  
         * @param keyname 
         * @param keyvalue 
         */  
        public void put(String keyname, Object keyvalue) {  
            // 處理中文亂碼  
            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) {  
            }  
        }  
      
        /** 
         * 更新 
         *  
         * @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) {  
            }  
        }  
      
        /** 
         * 根據(jù)key刪除 
         *  
         * @param key 
         */  
        public void delete(String key) {  
            try {  
                FileInputStream fis = new FileInputStream(filepath);  
                properties.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);  
                properties.remove(key);  
                for (java.util.Map.Entry<String, String> entry : map.entrySet()) {  
                    properties.setProperty(entry.getKey(), entry.getValue());  
                }  
                FileOutputStream fos = new FileOutputStream(filepath);  
                properties.store(fos, "delete key:" + key);  
                fos.close();  
                fis.close();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
    }  

使用方法

    // 這里獲取LocalStorage對(duì)象。參數(shù)也可以為文件絕對(duì)路徑,當(dāng)然也可以直接傳入Context和文件名。  
        LocalStorage localStorage = LocalStorage.get("test.txt");  
        // 增加  
        localStorage.put("key", "哈哈");  
        // 更新  
        localStorage.update("key", "value");  
        // 查找,這里提供多個(gè)getAs 方法。取數(shù)據(jù)找到相應(yīng)的數(shù)據(jù)類型  
        localStorage.getAsString("key");  
      
        // 刪除  
        localStorage.delete("key");  

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:Android自帶的下拉刷新組件SwipeRefreshLayout

下一篇:android將閃光燈作為手電筒的實(shí)現(xiàn)代碼