Android SD card Read Simple operation

Source: Internet
Author: User

Package Com.example.sdcardtest;import Java.io.bufferedreader;import Java.io.file;import java.io.FileInputStream; Import Java.io.filenotfoundexception;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.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;public class Mainactivity extends Activity {final String file_name = "/baidu/hello.txt"; overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Button read = (button) Findviewbyid (R.id.read); Button write = (button) Findviewbyid (r.id.write), final EditText edit1 = (EditText) Findviewbyid (R.ID.EDITTEXT1); final EditText edit2 = (EditText) Findviewbyid (R.ID.EDITTEXT2);//For write event binding listener Write.setonclicklistener (new Onclicklistener () {public void OnClick (View v) {Write (edit1.gEttext (). toString ()); Edit1.settext ("");}}); /For the Read event binding Listener Read.setonclicklistener (new Onclicklistener () {public void OnClick (View v) {edit2.settext (read ());}});} Public String Read () {if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {File Sdcarddir = Environment.getexternalstoragedirectory (); try {fileinputstream fis = new FileInputStream (sdcarddir.getcanonicalpath () + file_name); BufferedReader br = new BufferedReader (new InputStreamReader (FIS)); StringBuilder sb = new StringBuilder (""); String line = null;while (line = Br.readline ())! = null) {sb.append (line);} Br.close (); return sb.tostring ();} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ()}} return null;} public void Write (String content) {if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) { Randomaccessfile RAF = Null;try {//Get the SD card directory file Sdcarddir = Environment.getexternalstoragedirectory (); File TargetFile = new file (sdcarddir.getcanonicalfilE () + file_name), RAF = new Randomaccessfile (targetfile, "RW"), Raf.seek (Targetfile.length ()); Raf.write ( Content.getbytes ());} catch (IOException e) {e.printstacktrace ();} try {raf.close ();} catch (IOException e) {e.printstacktrace ();}}}} ===================== layout file Simple two buttons, nothing to say

Android SD card Read Simple operation

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.