文件緩存(配合JSON數(shù)組)
更新時間:2016年11月27日 17:17:26 作者:妖久
這篇文章主要介紹了文件緩存(配合JSON數(shù)組)的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
1. 寫入緩存:建立文件夾,把list集合里面的數(shù)組轉換為JSON數(shù)組,存入文件夾
2. 讀取緩存:把JSON數(shù)組從文件夾里面讀取出來,然后放入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ù)組),希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
相關文章
Android使用Sensor感應器實現(xiàn)線程中刷新UI創(chuàng)建android測力計的功能
這篇文章主要介紹了Android使用Sensor感應器實現(xiàn)線程中刷新UI創(chuàng)建android測力計的功能,實例分析了Android使用Sensor感應器實現(xiàn)UI刷新及創(chuàng)建測力器的技巧,需要的朋友可以參考下2015-12-12
Android使用RecyclerView實現(xiàn)今日頭條頻道管理功能
這篇文章主要為大家詳細介紹了Android使用RecyclerView實現(xiàn)今日頭條頻道管理功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
android實現(xiàn)視頻的加密和解密(使用AES)
本篇文章主要介紹了android實現(xiàn)視頻的加密和解密(使用AES),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05

