Reading and storing SD card files for Android Development

Source: Internet
Author: User
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" />

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.