Android import and export via JSON

Source: Internet
Author: User

1 //Export File2         Private voidExport () {3FilePath =NULL;4  5                 //determine if the SD card exists6                 BooleanHassdcard =environment.getexternalstoragestate (). Equals (7 environment.media_mounted);8                 if(hassdcard) {9  TenFilePath =environment.getexternalstoragedirectory (). toString () One+ File.separator + "Hello.txt"; A   -                         Try { -File =NewFile (Environment.getexternalstoragedirectory (), the"Hello.txt"); -                                 if(File.exists ()) { - File.delete (); -                                 } +FileOutputStream OutStream =Newfileoutputstream (file); -Listnote =Createdata.getlistdata (); +Jsonarray Jsonarray =NewJsonarray (); A                                 Try { at                                          for(inti = 0; I < listnote.size (); i++) { -JSON =NewJsonobject (); -Bug.print ("" +listnote.size ()); -SYSTEM.OUT.PRINTLN ("---->" +listnote.size ()); -Json.put ("Id", Listnote.get (i). GetId ()); -Json.put ("Colorid", Listnote.get (i). Getcolorid ()); inJson.put ("Context", Listnote.get (i). GetContext ()); -Json.put ("Time", Listnote.get (i). GetTime ()); toBug.print (json.getstring ("Colorid")); + Jsonarray.put (JSON); -                                         } theString s =jsonarray.tostring (); * System.out.println (s); $ Outstream.write (S.getbytes ());Panax Notoginseng}Catch(jsonexception e) { - e.printstacktrace (); the   +                                 } AToast.maketext ( This. Owner, "", Toast.length_long). Show (); the outstream.close (); + file.createnewfile (); -   $}Catch(Exception e) { $ e.printstacktrace (); - Bug.print (E.getmessage ()); -                         } the                         //Prompt Box -Alertdialog.builder Builder =NewBuilder ( This. Owner);WuyiBuilder.setmessage ("Export succeeded"); the   -Builder.settitle ("Text file (hello.txt) is output to SD card" + file + "directory"); Wu   -Builder.setpositivebutton ("Confirm",NewOnclicklistener () { About @Override $                                  Public voidOnClick (Dialoginterface Dialog,intwhich) { - Dialog.dismiss (); -                                 } -                         }); A builder.create (). Show (); +                 } the         } -   $   the   the   the         //import File the         Private voidIntroduction () { -                 /* in                    the                  */ the                 Try { About   theFile Urlfile =NewFile (environment.getexternalstoragedirectory () the+ "/hello.txt"); theInputStreamReader ISR =NewInputStreamReader (NewFileInputStream ( +Urlfile), "UTF-8"); -BufferedReader br =NewBufferedReader (ISR); thestr = "";BayiString Mimetypeline =NULL; the   the                          while((Mimetypeline = Br.readline ())! =NULL) { -str = str +Mimetypeline; -   the                         } the System.out.println (str); theJsonobject obj =NULL; theJsonarray Jsonarray =NULL; -   theJsonarray =NewJsonarray (str); the                          for(inti = 0; I < jsonarray.length (); i++) { theobj =Jsonarray.getjsonobject (i);94   theStrid = obj.getstring ("Context"); theTime = obj.getstring ("Time"); theColorid = obj.getstring ("Colorid");98   About System.out.println (Strid); -Createdata.insetdata (NewEntity (Strid, Time, Colorid));101                         }102  103LOG.D ("TAG", str);104}Catch(Exception e) { the  106Toast.maketext ( This. Owner, "Import Failed", Toast.length_long). Show ();107                 }108}

Android import and export via 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.