天天看點

安卓檔案儲存、讀取工具類,可以直接用,包含路徑說明

/**
 * 描述:工具類,用于檔案的儲存,讀取
 * 建立人:菜籽
 * 建立時間:2017/7/26 下午5:36
 * 備注:
 */

public class FileUtils {

    private final static String SHARED_PREFERENCE_FILE_NAME = "information";

    /**
     * 将檔案儲存到APP安裝目錄下的緩存中
     * 從安卓整潔度考慮,不要把緩存檔案放到公共路徑下,
     * 說實話,我很鄙視這種在SD卡根目錄建立檔案夾的行為
     * 如果要長久儲存,則放在getExternalFilesDir(null)中
     * 如果是作為緩存,則放在getExternalCacheDir()中
     * 儲存到私有存儲的話,不需要申請額外權限
     *
     * context.getExternalFilesDir(null)————/storage/emulated/0/Android/包名/files
     *
     * context.getExternalCacheDir()————————/storage/emulated/0/Android/包名/cache
     *
     * context.getFilesDir()————————————————/data/data/包名/files
     *
     * context.getCacheDir()————————————————/data/data/包名/cache
     *
     * context.getExternalFilesDir(null)————共有存儲目錄,跟SD卡中Android目錄同級,需要申請讀寫權限
     *
     * 注意:調用以上接口儲存的資料,在APP下載下傳之後,資料會随之删除,不留垃圾
     * 注意:data 分區十分有限,不建議把大型資料儲存在 data 分區下
     * 注意:Google建議把資料儲存在/storage/emulated/0/Android/包名/files下
     * 注意:上面方法中需要填寫參數的接口,内部的參數可以指定子檔案夾,參數可以放Environment.DIRECTORY_PICTURES之類的
     *      比如context.getExternalFilesDir(Environment.DIRECTORY_DCIM)
     *
     *
     * @param fileName : 儲存之後的檔案名字
     * @param bytes    : 要儲存的檔案的位元組流
     * @return
     */
    public static boolean saveFileToInnerStorage(Context context, String fileName, byte[] bytes) {
        File file = new File(context.getExternalCacheDir(), fileName);
        if (file.exists()) {
            file.delete();
        }
        try {
            FileOutputStream outputStream = new FileOutputStream(file);
            outputStream.write(bytes);

            outputStream.flush();
            outputStream.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 從APP安裝目錄下的緩存中擷取儲存的檔案
     *
     * @param context
     * @param fileName
     * @return
     */
    public static byte[] readFileFromInnerStorage(Context context, String fileName) {
        File file = new File(context.getExternalCacheDir(), fileName);
        try {
            FileInputStream inputStream = new FileInputStream(file);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
            byte[] array = outputStream.toByteArray();
            return array;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    //********通過SharedPreference儲存資料,儲存路徑為/data/data/包名/XX
    public static boolean saveDataWithSharedPreference(Context context, String key, String value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFERENCE_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        return editor.commit();
    }

    public static boolean saveDataWithSharedPreference(Context context, String key, int value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFERENCE_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    public static boolean saveDataWithSharedPreference(Context context, String key, boolean value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFERENCE_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }

    //*******通過SharedPreference讀取資料
    public static String readDataWithSharedPreference(Context context, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFERENCE_FILE_NAME, Context.MODE_PRIVATE);
        String value = sharedPreferences.getString(key, null);
        return value;
    }

    public static int readDataWithSharedPreference(Context context, String key, int defaultValue) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFERENCE_FILE_NAME, Context.MODE_PRIVATE);
        int value = sharedPreferences.getInt(key, defaultValue);
        return value;
    }

    public static boolean readDataWithSharedPreference(Context context, String key, boolean flag) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFERENCE_FILE_NAME, Context.MODE_PRIVATE);
        boolean value = sharedPreferences.getBoolean(key, flag);
        return value;
    }

}