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