Android read SD card file

Source: Internet
Author: User

 Public classSdcardtest extends activity{final String file_name="/crazyit.bin"; @Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main); //get two buttonsButton Read =(Button) Findviewbyid (R.id.read); Button Write=(Button) Findviewbyid (r.id.write); //get two text boxesFinal EditText edit1 =(EditText) Findviewbyid (R.ID.EDIT1); Final EditText Edit2=(EditText) Findviewbyid (R.ID.EDIT2); //to bind an event listener to the Write buttonWrite.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View source) {//writing content from Edit1 to a fileWrite (Edit1.gettext (). toString ()); Edit1.settext ("");        }        }); Read.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//reads the contents of the specified file and displays itEdit2.settext (read ());    }        }); }    PrivateString Read () {Try        {            //if the phone is plugged into an SD card and the app has access to SD            if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) { //get the storage directory for the SD cardFile Sdcarddir =environment.getexternalstoragedirectory (); //gets the input stream corresponding to the specified fileFileInputStream FIS =NewFileInputStream (Sdcarddir.getcanonicalpath ()+file_name); //wraps the specified input stream into BufferedReaderBufferedReader br =NewBufferedReader (NewInputStreamReader (FIS)); StringBuilder SB=NewStringBuilder (""); String Line=NULL; //looping through the contents of a file                 while(line = Br.readline ())! =NULL) {sb.append (line); }                //Close ResourceBr.close (); returnsb.tostring (); }        }        Catch(Exception e) {e.printstacktrace (); }        return NULL; }    Private voidWrite (String content) {Try        {            //if the phone is plugged into an SD card and the app has access to SD            if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) { //get the SD card directoryFile Sdcarddir =environment.getexternalstoragedirectory (); File TargetFile=NewFile (Sdcarddir. Getcanonicalpath ()+file_name); //Create a Randomaccessfile object with the specified fileRandomaccessfile RAF =Newrandomaccessfile (TargetFile,"RW"); //move the file record pointer to the lastRaf.seek (Targetfile.length ()); //Output File ContentsRaf.write (Content.getbytes ()); //Close RandomaccessfileRaf.close (); }        }        Catch(Exception e) {e.printstacktrace (); }    }}

Android read SD card file

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.