Read and Write Android text files (including SD cards)

Source: Internet
Author: User

-----------------------------------------------

Package com. eboy. filereadwrite;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. OS. environment;
Import Android. View. view;
Import Android. widget. Button;
Import Android. widget. edittext;
Import Android. widget. Toast;

Import com. eboy. filereadwrite. R. ID;
Import com. eboy. filereadwrite. R. String;
Import com. eboy. Service. fileservice;

Public class mainactivity extends activity {
Private edittext edtfilename = NULL;
Private edittext edtfilecontent = NULL;
Private button btnread = NULL;
Private button btnwrite = NULL;
Private button btnsdread = NULL;
Private button btnsdwrite = NULL;
Private string filename;
Private string filecontent;

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. activity_main );

Edtfilename = (edittext) findviewbyid (R. Id. filename );
Edtfilecontent = (edittext) findviewbyid (R. Id. filecontent );
Btnread = (button) findviewbyid (Id. btnread );
Btnwrite = (button) findviewbyid (Id. btnwrite );
Btnread. setonclicklistener (New readonclicklistener ());
Btnwrite. setonclicklistener (New writeonclicklistener ());

Btnsdread = (button) findviewbyid (Id. btnsdread );
Btnsdwrite = (button) findviewbyid (Id. btnsdwrite );
Btnsdread. setonclicklistener (New sdreadonclicklistener ());
Btnsdwrite. setonclicklistener (New sdwriteonclicklistener ());
}

Private final class writeonclicklistener implements view. onclicklistener {

Public void onclick (view v ){
Fileservice service = new fileservice (getapplicationcontext ());
Filename = edtfilename. gettext (). tostring ();
Filecontent = edtfilecontent. gettext (). tostring ();
Try {
Service. Write (filename, filecontent );
Toast. maketext (getapplicationcontext (), String. writesuccess, Toast. length_long). Show ();
}
Catch (exception e ){
Toast. maketext (getapplicationcontext (), String. writefail + "," + E. tostring (), Toast. length_long). Show ();
}
}
}

Private final class readonclicklistener implements view. onclicklistener {

Public void onclick (view v ){
Fileservice service = new fileservice (getapplicationcontext ());
Filename = edtfilename. gettext (). tostring ();
Filecontent = edtfilecontent. gettext (). tostring ();
Try {
Filecontent = service. Read (filename );
Edtfilecontent. settext (filecontent );
Toast. maketext (getapplicationcontext (), String. readsuccess, Toast. length_long). Show ();
}
Catch (exception e ){
Toast. maketext (getapplicationcontext (), String. readfail + "," + E. tostring (), Toast. length_long). Show ();
}
}
}

Private final class sdwriteonclicklistener implements view. onclicklistener {

Public void onclick (view v ){
Fileservice service = new fileservice (getapplicationcontext ());
Filename = edtfilename. gettext (). tostring ();
Filecontent = edtfilecontent. gettext (). tostring ();
Try {
// Determine whether the SD card exists
If (environment. getexternalstoragestate (). Equals (environment. media_mounted )){
Service. writetosdcard (filename, filecontent );
} Else {
Toast. maketext (getapplicationcontext (), String. sdcardnotfound, Toast. length_long). Show ();
}
Toast. maketext (getapplicationcontext (), String. writesuccess, Toast. length_long). Show ();
}
Catch (exception e ){
Toast. maketext (getapplicationcontext (), String. writefail + "," + E. tostring (), Toast. length_long). Show ();
}
}
}

Private final class sdreadonclicklistener implements view. onclicklistener {

Public void onclick (view v ){
Fileservice service = new fileservice (getapplicationcontext ());
Filename = edtfilename. gettext (). tostring ();
Filecontent = edtfilecontent. gettext (). tostring ();
Try {
If (environment. getexternalstoragestate (). Equals (environment. media_mounted )){
Filecontent = service. readfromsdcard (filename );
Edtfilecontent. settext (filecontent );
Toast. maketext (getapplicationcontext (), String. readsuccess, Toast. length_long). Show ();
} Else {
Toast. maketext (getapplicationcontext (), String. sdcardnotfound, Toast. length_long). Show ();
}
}
Catch (exception e ){
Toast. maketext (getapplicationcontext (), String. readfail + "," + E. tostring (), Toast. length_long). Show ();
}
}
}

/Files/jxgxy/filereadwrite.rar

Related Article

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.