Android read-write SD card operation write data read Data sample _android

Source: Internet
Author: User

Copy Code code as follows:

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.os.Bundle;
Import android.os.Environment;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
Import android.app.Activity;

Public class Mainactivity extends activity {
    final String file_name = "/lxp.bin";
    @Override
    protected void onCreate (Bundle savedinstancestate) {
         super.oncreate (savedinstancestate);
        Setcontentview (R.layout.main);
        Button Write = (button) Findviewbyid (R.ID.MAIN_BTN1);
        Button Read = (button) Findviewbyid (R.ID.MAIN_BTN2);
        Final EditText edit1 = (edittext) Findviewbyid (R.ID.MAIN_EDIT1);
        Final EditText edit2 = (edittext) Findviewbyid (R.ID.MAIN_EDIT2);
        Write.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
Write (Edit1.gettext (). toString (). Trim ());
Edit1.settext ("");
}
});
Read.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
Edit2.settext (read ());
}
});
}
private void Write (String content) {
try {
if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {
Get the directory of the SD card
File Sdcarddir = Environment.getexternalstoragedirectory ();
File TargetFile = new file (Sdcarddir.getcanonicalpath () + file_name);
Creates a Randomaccessfile object with the specified file
Randomaccessfile RAF = new Randomaccessfile (targetfile, "RW");
Move the file record pointer to the last
Raf.seek (Targetfile.length ());
Output file contents
Raf.write (Content.getbytes ());
Raf.close ();
}
catch (Exception e) {
E.printstacktrace ();
}
}

Private String Read () {
try {
if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {
Obtain the corresponding storage directory of SD card
File Sdcarddir = Environment.getexternalstoragedirectory ();
Gets the input stream corresponding to the specified file
FileInputStream fis = new FileInputStream (Sdcarddir.getcanonicalpath () + file_name);
Wraps the specified input stream into a bufferreader
BufferedReader br = new BufferedReader (new InputStreamReader (FIS));
StringBuilder sb = new StringBuilder ("");
String line = null;
Iterate over the contents of a file
while (line = Br.readline ())!= null) {
Sb.append (line);
}
Br.close ();
return sb.tostring ();
}
catch (Exception e) {
E.printstacktrace ();
}
return null;
}
}

Finally, don't forget to increase access to read SD in Androidmanafest.xml.

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.