数据缓存cache中

来源:互联网 发布:优化经济发展环境 编辑:程序博客网 时间:2024/05/20 19:15


1、 //得到cache的路径
    public String getDiskCachePath(Context context){
        if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())||!Environment.isExternalStorageRemovable())
        {
            //如果sd卡存在并且sd卡没有移除,返回外部的cache
            return context.getExternalCacheDir().getPath();
        }else
        {//返回内部的cache
            return context.getCacheDir().getPath();
        }

    }



 //解析的json串存入cache
                        //得到路径
                        String cachePath = getDiskCachePath(MainActivity.this);
                        System.out.println("========" + cachePath);
                        //存入数据
                        File file = new File(cachePath + "/json");
                        OutputStream os = new FileOutputStream(file);
                        os.write(s.getBytes());
                        os.flush();

原创粉丝点击