File cache (with JSON array)

Source: Internet
Author: User

   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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.