Android store JSON to local, and read local JSON

Source: Internet
Author: User

/*** Save JSON to local *@parammactivity *@paramfilename *@paramcontent*/     Public StaticFile dir =NewFile (Environment.getexternalstoragedirectory () + "/. imageloader/json/");  Public Static voidSavetosdcard (Activity mactivity, string filename, string content) {string en=environment.getexternalstoragestate (); //gets the SDcard status if SDcard is plugged into a phone and is not write protected        if(En.equals (environment.media_mounted)) {Try{dir.mkdirs ();//Create folders where write filesFile File =NewFile (dir, filename); OutputStream out=Newfileoutputstream (file);                Out.write (Content.getbytes ()); Out.close ();Apputils.showtoast (mactivity, "save Success");}Catch(Exception e) {e.printstacktrace ();Apputils.showtoast (mactivity, "Save Failed");            }        } Else {            //prompt for user sdcard not present or write protected statusApputils.showtoast (mactivity, "sdcard not present or write protected State"); }    }    /*** Read JSON from the local *@parammactivity *@paramfilename *@paramcontent*/     Public Staticstring ReadTextFile (String filePath) {StringBuilder sb=NewStringBuilder (); Try{File File=NewFile (dir + "/" +FilePath); InputStream in=NULL; Inch=Newfileinputstream (file); intTempbyte;  while((Tempbyte = In.read ())! =-1) {sb.append (Char) tempbyte);        } in.close (); } Catch(Exception e) {e.printstacktrace (); }        returnsb.tostring (); }

Android store JSON to local, and read local JSON

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.