文件緩存(配合JSON數(shù)組)
1. 寫入緩存:建立文件夾,把list集合里面的數(shù)組轉(zhuǎn)換為JSON數(shù)組,存入文件夾
2. 讀取緩存:把JSON數(shù)組從文件夾里面讀取出來(lái),然后放入list集合,返回list集合
private final static File filefolder=new File("/sdcard/myData");
private final static File filename=new File("/sdcard/myData/tem.txt");
public static boolean writeCache(List<Data> list)
{
if(!filefolder.exists())
filefolder.mkdirs();
try
{
JSONArray array=new JSONArray();
for(int i=0;i<list.size();i++)
{
Data data=list.get(i);
JSONObject ob=new JSONObject();
ob.put("name", data.getName());
ob.put("reason", data.getReason());
array.put(ob);
}
FileWriter fw=new FileWriter(filename);
fw.write(array.toString());
fw.close();
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
public static List<Data> readCache() throws JSONException,IOException
{
if(!filefolder.exists())
filefolder.mkdir();
List<Data> list=new ArrayList<Data>();
if(filename.exists())
{
FileInputStream in=new FileInputStream(filename);
String line=null;
StringBuffer sb=new StringBuffer("");
BufferedReader br=new BufferedReader(new InputStreamReader(in));
while((line=br.readLine())!=null)
sb.append(line);
br.close();
in.close();
JSONArray array=new JSONArray(sb.toString());
for(int i=0;i<array.length();i++)
{
JSONObject ob=new JSONObject();
ob=array.getJSONObject(i);
Data data=new Data();
data.setName(ob.getString("name"));
data.setReason(ob.getString("reason"));
list.add(data);
}
}
return list;
}
以上所述是小編給大家介紹的文件緩存(配合JSON數(shù)組),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
- jquery利用json實(shí)現(xiàn)頁(yè)面之間傳值的實(shí)例解析
- php+ajax+json 詳解及實(shí)例代碼
- C#中實(shí)現(xiàn)Json序列化與反序列化的幾種方式
- js中動(dòng)態(tài)創(chuàng)建json,動(dòng)態(tài)為json添加屬性、屬性值的實(shí)例
- AngularJS中的JSONP實(shí)例解析
- 將JSON字符串轉(zhuǎn)換成Map對(duì)象的方法
- ASPNET中JSON的序列化和反序列化的方法
- IOS開發(fā)之JSON轉(zhuǎn)PLIST實(shí)例詳解
- javascript學(xué)習(xí)之json入門
相關(guān)文章
Android?app啟動(dòng)節(jié)點(diǎn)與上報(bào)啟動(dòng)實(shí)例詳解
系統(tǒng)的啟動(dòng)過(guò)程非常復(fù)雜,下面這篇文章主要給大家介紹了關(guān)于Android?app啟動(dòng)節(jié)點(diǎn)與上報(bào)啟動(dòng)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
android相冊(cè)選擇圖片的編碼實(shí)現(xiàn)代碼
本篇文章主要介紹了android相冊(cè)選擇圖片的編碼實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
android點(diǎn)擊無(wú)效驗(yàn)證的解決方法
這篇文章主要給大家介紹了關(guān)于android點(diǎn)擊無(wú)效驗(yàn)證的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Android使用Sensor感應(yīng)器實(shí)現(xiàn)線程中刷新UI創(chuàng)建android測(cè)力計(jì)的功能
這篇文章主要介紹了Android使用Sensor感應(yīng)器實(shí)現(xiàn)線程中刷新UI創(chuàng)建android測(cè)力計(jì)的功能,實(shí)例分析了Android使用Sensor感應(yīng)器實(shí)現(xiàn)UI刷新及創(chuàng)建測(cè)力器的技巧,需要的朋友可以參考下2015-12-12
Android使用RecyclerView實(shí)現(xiàn)今日頭條頻道管理功能
這篇文章主要為大家詳細(xì)介紹了Android使用RecyclerView實(shí)現(xiàn)今日頭條頻道管理功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
android實(shí)現(xiàn)視頻的加密和解密(使用AES)
本篇文章主要介紹了android實(shí)現(xiàn)視頻的加密和解密(使用AES),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05

