File Storage:
Public class mainactivity extends activity {edittext mname, Mage; textview MTV; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); mname = (edittext) findviewbyid (R. id. edittext1); mage = (edittext) findviewbyid (R. id. edittext2); MTV = (textview) findviewbyid (R. id. textview1);} public void onclick (view v) {string name = mname. gettext (). tostring (); int age = integer. parseint (mage. gettext (). tostring (); string cont = "name =" + name + ", age =" + age + "\ n"; try {int id = v. GETID (); // internally save if (ID = R. id. button1) {fileoutputstream Fos = This. openfileoutput ("mytext.txt", context. mode_append | context. mode_world_writeable | context. mode_world_readable); FOS. write (Cont. getbytes (); FOS. close (); toast. maketext (this, "Write completed", 1 ). show () ;}// read else if (ID = R. id. button2) {fileinputstream FCM = This. openfileinput ("mytext.txt"); byte [] bytes = new byte [FCM. available ()]; FCM. read (bytes); FCM. close (); string STR = new string (bytes); MTV. settext (STR) ;}} catch (exception e) {e. printstacktrace ();}}
If you want to upload this mytext.txt file, the format is as follows:
Public class mainactivity extends activity {textview mcontent; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); mcontent = (textview) findviewbyid (R. id. textview1);} public void onclick (view v) {Switch (v. GETID () {case R. id. button1: Try {readremotefilebyabslutepath ();} catch (exception e) {// todo auto-gen Erated Catch Block E. printstacktrace ();} break; case R. id. button2: Try {writeremotefilebyabslutepath ();} catch (exception e) {// todo auto-generated Catch Block E. printstacktrace ();} break; default: break;}/*** read the remote file through the absolute path of the file * @ throws exception */Public void readremotefilebyabslutepath () throws exception {string Path = "/data/COM. nanguabing. filedemo/files/mytext.txt "; fileinputst Ream FD = new fileinputstream (PATH); byte [] bytes = new byte [FCM. available ()]; FCM. read (bytes); FCM. close (); string STR = new string (bytes); mcontent. settext (STR); log. I ("other", STR);}/*** read the remote file through the absolute path of the file * @ throws exception */Public void writeremotefilebyabslutepath () throws exception {string Path = "/data/COM. nanguabing. filedemo/files/mytext.txt "; fileoutputstream Fos = new fileoutput Stream (path, true); FOS. Write ("Other write! ". Getbytes (); FOS. Close (); log. I (" other "," Other Write over! ");}/*** Write a remote file through the package-related context * @ throws exception */Public void readromotebypackagecontext () throws exception {string pname =" com. nanguabing. filedemo "; Context CTX = This. createpackagecontext (pname, context. context_ignore_security); fileinputstream FCM = CTX. openfileinput ("mytext.txt"); byte [] bytes = new byte [FCM. available ()]; FCM. read (bytes); FCM. close (); log. I ("other", new string (bytes);}/*** write a remote file through the package context */Public void readromotebypackagecontext2 () throws exception {string pname = "com. nanguabing. filedemo "; Context CTX = This. createpackagecontext (pname, context. context_include_code); fileinputstream FCM = CTX. openfileinput ("mytext.txt"); byte [] bytes = new byte [FCM. available ()]; FCM. read (bytes); FCM. close (); log. I ("other", new string (bytes) ;}@ override public Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. activity_main, menu); Return true ;}}
Mobile development QQ group: 59516399
Http://download.csdn.net/detail/wenwei19861106/4975929 (csdn)