数据持久化方案(四)文件保存

文件保存,也算是比较常见的,也比较方便的保存方式。
在Android中,我们可以通过文件保存json数据来实现本地缓存

public class FileUtil {
    public static final String CACHE = "cache";
    public static final String ICON = "icon";
    public static final String ROOT = "csdn";


    public static void saveLocal(String json, int index) {
        BufferedWriter bw = null;
        try {
            File dir = FileUtil.getCacheDir();
            // 在第一行写一个过期时间
            File file = new File(dir, "_" + index); // /mnt/sdcard/googlePlay/cache/home_0
            FileWriter fw = new FileWriter(file);
            bw = new BufferedWriter(fw);
            bw.write(System.currentTimeMillis() + 1000 * 100 + "");
            bw.newLine();// 换行
            bw.write(json);// 把整个json文件保存起来
            bw.flush();
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(bw);
        }
    }

    public static String loadLocal(int index) {
        // 如果发现文件已经过期了 就不要再去复用缓存了
        File dir = FileUtil.getCacheDir();// 获取缓存所在的文件夹
        File file = new File(dir, "_" + index);
        try {
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            long outOfDate = Long.parseLong(br.readLine());
            if (System.currentTimeMillis() > outOfDate) {
                return null;
            } else {
                String str = null;
                StringWriter sw = new StringWriter();
                while ((str = br.readLine()) != null) {
                    sw.write(str);
                }
                return sw.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 获取图片的缓存的路径
     *
     * @return /mnt/sdcard/csdn/icon     或者/data/data/com.csdndatasave/cache/icon
     */
    public static File getIconDir() {
        return getDir(ICON);
    }

    /**
     * 获取缓存路径   或者 /data/data/com.csdndatasave/cache/cache
     *
     * @return /mnt/sdcard/csdn/cache
     */
    public static File getCacheDir() {
        return getDir(CACHE);
    }

    public static File getDir(String cache) {
        StringBuilder path = new StringBuilder();
        if (isSDAvailable()) {
            path.append(Environment.getExternalStorageDirectory()
                    .getAbsolutePath());
            path.append(File.separator);// '/'
            path.append(ROOT);// /mnt/sdcard/
            path.append(File.separator);
            path.append(cache);// /mnt/sdcard/GooglePlay/cache

        } else {
            File filesDir = APP.getInstance().getCacheDir();    //  cache  getFileDir file
            path.append(filesDir.getAbsolutePath());// /data/data/com.itheima.googleplay/cache
            path.append(File.separator);///data/data/com.itheima.googleplay/cache/
            path.append(cache);///data/data/com.csdndatasave/cache/cache
        }
        File file = new File(path.toString());
        if (!file.exists() || !file.isDirectory()) {
            file.mkdirs();// 创建文件夹
        }
        return file;

    }

    private static boolean isSDAvailable() {
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

接下来我们只要去调用saveDir和loadDir就可以啦

 FileUtil.saveLocal(myData,1);
 String local = FileUtil.loadLocal(1);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值