Read and write SD card files

Source: Internet
Author: User

In order to better save and fetch large file data in the application, the application needs to read, write, and file on the SD card. SD card greatly expands the storage capacity of the mobile phone.
Read, write the file on the SD card greet the following steps to proceed. Call Environment's Getexternalstoragestate () method to determine if an SD card is plugged into the phone, and the application has permission to read and write to the SD card. Code hints are as follows:

if (Environment.getexternalstoragestate (). Equals (environment.media_mounted))

(2) Call Environment's getExternalStorageDirectory () method to obtain the external memory, which is the directory of the SD card.
(3) Use FileInputStream Fileoutstream filereader or FileWriter read, write the file inside the SD card

In order to read and write the data on the SD card, you must add read, write the SD card permissions in the application's manifest file (androidmanifest.xml). <uses-permission android:name= " Android.permission.MOUNT_UNMOUNT_FILESYSTEMS "/>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

Package com.test.sdcardtest;
Import Android.os.Bundle;
Import android.os.Environment;
Import android.support.v7.app.AppCompatActivity;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.EditText;

Import Android.widget.TextView;
Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;

Import Java.io.RandomAccessFile;
    /** * Read and write files on SD card */public class Mainactivity extends Appcompatactivity {Button btnwrite, btnread;
    EditText Etwrite;

TextView tvShow;
    Final String file_name = "/sd.txt";
        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

        Setcontentview (R.layout.activity_main);
        Btnwrite = (Button) Findviewbyid (r.id.btn_write);

        Btnread = (Button) Findviewbyid (R.id.btn_read);
        TvShow = (TextView) Findviewbyid (R.id.tv_read); Etwrite = (EditText) Findviewbyid (R.id.et_write);
                Btnwrite.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {

                Write (Etwrite.gettext (). toString ());
            Etwrite.settext ("");

        }
        });
                Btnread.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {
            Tvshow.settext (Read2 ());
    }
        }); } private String Read2 () {//If the phone has an SD card installed (Environment.getexternalstoragestate (). Equals (Environme Nt.
            media_mounted) {//get the corresponding storage directory for SD card File sdcarddir = Environment.getexternalstoragedirectory (); Gets the input stream corresponding to the specified file try {fileinputstream fis = new FileInputStream (sdcarddir.getcanonical
                Path () +file_name);

                Wraps the specified input stream into BufferedReader bufferedreader br = new BufferedReader (new InputStreamReader (FIS)); StringBuilder sb = new StringBuilder ("");
                String line = null;
                Loop reads the contents of the file while ((Line=br.readline ())!=null) {sb.append (line);
                }//Close resource br.close ();

            return sb.tostring ();
            } catch (IOException e) {e.printstacktrace ();
    }} return null; } private void Write (String s) {//Determine if the phone has an SD card installed if (Environment.getexternalstoragestate (). Equals (Envi Ronment.

            media_mounted) {//Get the SD card directory File Sdcarddir = Environment.getexternalstoragedirectory ();

                try {file targetfile =new file (Sdcarddir.getcanonicalpath () +file_name);
                Creates a Randomaccessfile object with the specified file Randomaccessfile RDF = new Randomaccessfile (targetfile, "RW");
                Moves the file record pointer to the last Rdf.seek (Targetfile.length ());
        Output file        Rdf.write (S.getbytes ());
            Rdf.close ();
            } catch (IOException e) {e.printstacktrace ();
 }
        }
    }
}

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.