写入文本:
//resData可以为Map类型或者是JSONObject类型,给resData一个标识,读取的时候方便判断
File cacheFile=new File(new File(PathKit.getRootClassPath()).getParentFile().getParentFile().getAbsolutePath()+"/cache/bigSegCommon.json");
FileUtil.writeUtf8String(resData.toString(), cacheFile);读取文本:
File cacheFile=new File(new File(PathKit.getRootClassPath()).getParentFile().getParentFile().getAbsolutePath()+"/cache/bigSegCommon.json");
if(!FileUtil.exist(cacheFile)){
FileUtil.touch(cacheFile);
FileUtil.writeUtf8String("{}", cacheFile);
}
String cacheData = FileUtil.readUtf8String(cacheFile);
JSONObject cacheJson = JSONObject.fromObject(cacheData);
if(cacheJson.get("date") != null&&cacheJson.get("date").toString().equals(nowData)){ return cacheJson; }注意:写入的时候可以先转化为jsonObject再调用toString()方法;
本文介绍了一种基于文件系统的缓存实现方法,通过将数据转换为JSON格式并使用FileUtil工具类进行读写操作。文章详细展示了如何创建缓存文件、写入默认内容以及从缓存中读取数据并解析为JSON对象。
3770

被折叠的 条评论
为什么被折叠?



