Android SD card Read/write

Source: Internet
Author: User

Package com.jredu.zuiyou.activity;

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.Toast;

Import COM.JREDU.ZUIYOU.R;
Import com.jredu.zuiyou.entity.Student;

Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.ObjectInputStream;
Import Java.io.ObjectOutputStream;

public class Sdactivity extends Appcompatactivity {
ObjectOutputStream oos = null;
ObjectInputStream ois = null;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.LAYOUT.ACTIVITY_SD);
Button write = (button) Findviewbyid (r.id.write);
Write.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Writetosd ();
}
});
Button read = (button) Findviewbyid (R.id.read);
Read.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
READFROMSD ();
}
});
}
public void Writetosd () {
String state = Environment.getexternalstoragestate ();
if (State.equals (environment.media_mounted)) {
File root = Environment.getexternalstoragedirectory ();
File TargetDir = new file (Root,super.getpackagename ());
if (! Targetdir.exists ()) {
Targetdir.mkdir ();
}
try {
Oos = new ObjectOutputStream (new FileOutputStream (New File (TargetDir, "yxl.txt"));
Student Student = new Student ("Xiaoming", 18);
Oos.writeobject (student);
Toast.maketext (Getapplication (), "Write succeeded", Toast.length_short). Show ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
try {
Oos.flush ();
Oos.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}else {
Toast.maketext (Getapplication (), "No SD card found", Toast.length_short). Show ();
}
}
public void Readfromsd () {
String state = Environment.getexternalstoragestate ();
if (State.equals (environment.media_mounted)) {
File root = Environment.getexternalstoragedirectory ();
File TargetDir = new file (Root,super.getpackagename ());
if (! Targetdir.exists ()) {
Targetdir.mkdir ();
}
try {
OIS = new ObjectInputStream (new FileInputStream (New File (TargetDir, "/yxl.txt"));
Student Student = (Student) ois.readobject ();
Toast.maketext (Getapplication (), Student.getname (). toString () + "," +student.getage (), Toast.length_short). Show ();

} catch (IOException e) {
E.printstacktrace ();
} catch (ClassNotFoundException e) {
E.printstacktrace ();
}finally {
try {
Ois.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}else {
Toast.maketext (Getapplication (), "No SD card found", Toast.length_short). Show ();
}
}
}

Android SD card Read/write

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.