-----------------------------------------------
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