File storage on the second day of Android Development

Source: Internet
Author: User

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)

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.