加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

android 超轻量级数据存储类

发布时间:2020-12-14 23:19:23 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.I

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

    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本地存储 ,主要用于存储简单key value键值对。提供增、删、改、查方法。 可自定义路径 
     *  
     * @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 
         *            文件绝对路径 
         * @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) {  
            }  
        }  
      
        /** 
         * 根据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对象。参数也可以为文件绝对路径,当然也可以直接传入Context和文件名。  
        LocalStorage localStorage = LocalStorage.get("test.txt");  
        // 增加  
        localStorage.put("key","哈哈");  
        // 更新  
        localStorage.update("key","value");  
        // 查找,这里提供多个getAs 方法。取数据找到相应的数据类型  
        localStorage.getAsString("key");  
      
        // 删除  
        localStorage.delete("key");  

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读