1. Write cache: Create a folder, convert the array inside the list collection to a JSON array, and save the folder
2. Read cache: The JSON array is read from the folder, and then put into the list collection, return to the list collection
Private Final StaticFile filefolder=NewFile ("/sdcard/mydata");
Private Final StaticFile filename=NewFile ("/sdcard/mydata/tem.txt"); Public Static BooleanWritecache (list<data>list) { if(!filefolder.exists ()) filefolder.mkdirs (); Try{Jsonarray array=NewJsonarray (); for(intI=0;i<list.size (); i++) {Data Data=List.get (i); Jsonobject ob=NewJsonobject (); Ob.put ("Name", Data.getname ()); Ob.put ("Reason", Data.getreason ());array.put (OB); } FileWriter FW=NewFileWriter (filename); Fw.write (Array.tostring ()); Fw.close (); } Catch(Exception e) {e.printstacktrace (); return false; } return true; } Public StaticList<data> Readcache ()throwsjsonexception,ioexception {if(!filefolder.exists ()) Filefolder.mkdir (); List<Data> list=NewArraylist<data>(); if(Filename.exists ()) {FileInputStream in=Newfileinputstream (filename); String Line=NULL; StringBuffer SB=NewStringBuffer (""); BufferedReader BR=NewBufferedReader (NewInputStreamReader (in)); while((Line=br.readline ())! =NULL) Sb.append (line); Br.close (); In.close (); Jsonarray Array=NewJsonarray (sb.tostring ()); for(intI=0;i<array.length (); i++) {jsonobject ob=NewJsonobject (); OB=Array.getjsonobject (i); Data Data=NewData (); Data.setname (Ob.getstring ("Name"));Data.setreason (ob.getstring ("Reason")); list.add (data); } } returnlist; }
File cache (with JSON array)