android 文件存储&SharedPreferences

 

一、文件存储

文件存储主要是I/O流的操作,没什么好说的,需要注意的是保存文件的各个目录。

下面为常用的目录:

    public static File getInFileDir(Context context) {
        return context.getFilesDir(); // /data/data/packagename/files
    }

    public static File getInCacheDir(Context context) {
        return context.getCacheDir();// /data/data/packagename/cache
    }

    public static File getExRootDir() {
        return Environment.getExternalStorageDirectory();// /storage/sdcard0
    }

    public static File getExPrivateDir(Context context, String type) {
        return context.getExternalFilesDir(type); // /storage/sdcard0/Android/data/packagename/files/{type}
    }

    public static File getExPublicDir(String type) {
        return Environment.getExternalStoragePublicDirectory(type); // /storage/sdcard0/{type}
    }

    public static File getExCacheDir(Context context) {
        return context.getExternalCacheDir(); ///storage/sdcard0/Android/data/packagename/cache
    }

其中,除了getExRootDir()和getExPublicDir()两个目录,其余的目录中的数据都会随app的卸载而删除。

File getFilesDir()  //  /data/data/(packagename)/files

FileOutputStream openFileOutput(String name, int mode)

boolean deleteFile(String name)

String[] fileList()

上面的几个方法都是对目录/data/data/(packagename)/files的操作。


 

二、SharedPreferences

SharedPreferences适用于保存轻量级的数据,如配置文件。

SharedPreferences文件保存在/data/data/(packagename)/shared_prefs目录下,为xml格式的文件。

获取SharedPreferences:

SharedPreferences getPreferences(int mode)  //m1

SharedPreferences getSharedPreferences(String name, int mode)  //m2

可以通过上面两个方法获取SharePreferences,m1的文件名为调用此方法的类名,m2自己设置文件名。

 

保存数据:

        SharedPreferences sp = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("name", "mao");
        editor.apply();

获取数据:

       String name = sp.getString("name", "");

 

下面是一个简单的工具类:

public class SPUtils {

    @SuppressWarnings("unchecked")
    public static void put(Context context, String filename, String key, Object obj) {
        SharedPreferences sp = context.getSharedPreferences(filename, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        if (obj instanceof Boolean) {
            editor.putBoolean(key, (Boolean) obj);
        } else if (obj instanceof Float) {
            editor.putFloat(key, (Float) obj);
        } else if (obj instanceof Integer) {
            editor.putInt(key, (Integer) obj);
        } else if (obj instanceof Long) {
            editor.putLong(key, (Long) obj);
        } else if (obj instanceof String) {
            editor.putString(key, (String) obj);
        } else if (obj instanceof Set) {
            editor.putStringSet(key, (Set<String>) obj);
        }
        editor.apply();

    }

    @SuppressWarnings("unchecked")
    public static Object get(Context context, String filename, String key, Object defaultObj) {
        SharedPreferences sp = context.getSharedPreferences(filename, Context.MODE_PRIVATE);
        if (defaultObj instanceof Boolean) {
            return sp.getBoolean(key, (Boolean) defaultObj);
        } else if (defaultObj instanceof Float) {
            return sp.getFloat(key, (Float) defaultObj);
        } else if (defaultObj instanceof Integer) {
            return sp.getInt(key, (Integer) defaultObj);
        } else if (defaultObj instanceof Long) {
            return sp.getLong(key, (Long) defaultObj);
        } else if (defaultObj instanceof String) {
            return sp.getString(key, (String) defaultObj);
        }else if(defaultObj instanceof Set){
            return sp.getStringSet(key, (Set<String>) defaultObj);
        }
        return null;
    }

    public static void remove(Context context, String filename, String key) {
        SharedPreferences sp = context.getSharedPreferences(filename, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        editor.apply();
    }


    public static Map<String, ?> getAll(Context context, String filename) {
        SharedPreferences sp = context.getSharedPreferences(filename, Context.MODE_PRIVATE);
        Map<String, ?> map = sp.getAll();
        return map;
    }

    public static void clear(Context context, String filename) {
        SharedPreferences sp = context.getSharedPreferences(filename, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.apply();
    }


    public static boolean contains(Context context, String filename, String key) {
        SharedPreferences sp = context.getSharedPreferences(filename, Context.MODE_PRIVATE);
        return sp.contains(key);
    }

}

 

posted @ 2016-03-06 21:33  maozs  阅读(335)  评论(0编辑  收藏  举报