Package COM. example. demofile; import Java. io. bufferedreader; import Java. io. file; import Java. io. fileinputstream; import Java. io. ioexception; import Java. io. inputstreamreader; import Java. io. randomaccessfile; import android. app. activity; import android. OS. bundle; import android. OS. environment; import android. view. menu; import android. view. view; import android. view. view. onclicklistener; import android. widget. but Ton; import android. widget. textview; import android. widget. toast; public class mainactivity extends activity {private button read; private button write; private textview show; public static final string file_name = "Wang yuchao .txt"; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); super. setcontentview (R. layout. activity_main); read = (button) findviewbyid (R. id. button1 ); Write = (button) findviewbyid (R. id. button2); show = (textview) findviewbyid (R. id. textview1); read. setonclicklistener (New onclicklistener () {public void onclick (view v) {string STR = readsdcard (); If (STR = "error") {toast. maketext (mainactivity. this, "reading failed! ", Toast. length_short). Show (); show. settext (" reading failed! "); Return;} Show. settext (" read successful: "+ Str); toast. maketext (mainactivity. This," read successful! ", Toast. length_short ). show () ;}}); write. setonclicklistener (New onclicklistener () {public void onclick (view v) {string STR = show. gettext (). tostring (); If (writesdcard (STR) = true) {toast. maketext (mainactivity. this, "Write successful! ", Toast. length_short). Show (); return;} else {toast. maketext (mainactivity. This," Write failed! Check the SD card ", toast. length_short ). show (); Return ;}}) ;}protected Boolean writesdcard (string) {try {string sdcardstate = environment. getexternalstoragestate (); If (sdcardstate. equals (environment. media_mounted) = false) {return false;} // obtain the directory file sdcarddir = environment of the sdcard. getexternalstoragedirectory (); file targetfile = new file (sdcarddir. getCanonicalPath () + file. separator + file_name); If (! Targetfile. getparentfile (). exists () {targetfile. getparentfile (). mkdirs (); // create parent folder path} // create the randomaccessfile object randomaccessfile RAF = new randomaccessfile (targetfile, "RW"); RAF. seek (targetfile. length (); // move the pointer to the end, so that each write is an append () Raf. write (string. getbytes (); RAF. close ();} catch (ioexception e) {e. printstacktrace (); Return false;} return true;} private string readsdcard () {stringbuffer sb = ne W stringbuffer (); try {string sdcardstate = environment. getexternalstoragestate (); If (sdcardstate. equals (environment. media_mounted) = false) {return "error";} file sdcarddir = environment. getexternalstoragedirectory (); file targetfile = new file (sdcarddir. getCanonicalPath () + file. separator + file_name); If (! Targetfile. getparentfile (). exists () {targetfile. getparentfile (). mkdirs (); // create parent folder path} fileinputstream FD = new fileinputstream (targetfile); bufferedreader BR = new bufferedreader (New inputstreamreader (FCM); string line = NULL; while (line = BR. readline ())! = NULL) {sb. append (line);} BR. close (); FCM. close ();} catch (exception e) {e. printstacktrace (); Return "error";} return sb. tostring () ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {getmenuinflater (). inflate (R. menu. activity_main, menu); Return true ;}}
Permission issues must be specified when reading files.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />