android键值对

来源:互联网 发布:python.pdf 编辑:程序博客网 时间:2024/05/17 06:10
/** * 键值对写工具 */package  xx.utils;import java.io.IOException;import java.util.Map;import java.util.Map.Entry;import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;public class EditKeyValueUtils {    /**     * 将对象base64编码之后写入默认文件的指定的键     * @param context     * @param object 对象     * @param key 指定键     * @return     */    public boolean editObjectToLocal(Context context,Object object,String key){        return editObjectToLocal("application", context, object, key);    }    /**     *      * 将对象base64编码之后写入制定的键     *      * @param filePath 写入的文件名称     * @param context     * @param object 对象     * @param key 指定键     * @return     */    public boolean editObjectToLocal(String filePath,Context context,Object object,String key){        try {            String objectStr = new Base64Utils().encodetoBase64(object);            SharedPreferences preferences=context.getSharedPreferences(filePath, Activity.MODE_PRIVATE);            Editor editor=preferences.edit();            editor.putString(key, objectStr);            editor.commit();            return true;        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();            return false;        }    }    /**     * 从默认文件中取得指定键的值     * @param context     * @param key     * @return     */    public Object getObjectFromLocal(Context context,String key){        return getObjectFromLocal("application", context, key);    }    /**     * 从文件中取得指定键的值     * @param filePath     * @param context     * @param key     * @return     */    public Object getObjectFromLocal(String filePath,Context context,String key){        try {            SharedPreferences preferences=context.getSharedPreferences(filePath, Activity.MODE_PRIVATE);            String value=preferences.getString(key, "");            return new Base64Utils().decodeFromBase64(value);        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();            return null;        }    }    /**     * 清空默认文件键值对     * @param context     */    public void clearObjectLocal(Context context){        clearObjectLocal("application", context);    }    /**     * 清空文件键值对     * @param filePath     * @param context     */    public void clearObjectLocal(String filePath,Context context){        SharedPreferences preferences=context.getSharedPreferences(filePath, Context.MODE_PRIVATE);        Editor editor = preferences.edit();        Map<String, ?> map = preferences.getAll();        for(Entry<String, ?> set:map.entrySet()){            editor.remove(set.getKey());        }        editor.commit();    }    /**     * 移除键值对     * @param context     * @param key     */    public void removeObjectLocal(Context context,String key){        removeObjectLocal("application", context,key);    }    /**     * 移除指定文件键值对     * @param filePath     * @param context     * @param key     */    public void removeObjectLocal(String filePath,Context context,String key){        SharedPreferences preferences=context.getSharedPreferences(filePath, Context.MODE_PRIVATE);        Editor editor = preferences.edit();        editor.remove(key);        editor.commit();    }}
0 0
原创粉丝点击