SD卡写入和读取

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.study.R;
import com.jredu.study.entity.SdCard;

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 SdCardActivity extends AppCompatActivity {
    Button writeBtn;
    Button readBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sd_card);
        writeBtn = (Button) findViewById(R.id.writeSd);
        writeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WriteToSD();
            }
        });
        readBtn = (Button) findViewById(R.id.readSd);
        readBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ReadFromSD();
            }
        });
    }
    //向SD卡写入
    public void WriteToSD(){
        String state = Environment.getExternalStorageState();//获取外部存储状态
        ObjectOutputStream oos = null;
        if (state.equals(Environment.MEDIA_MOUNTED)) {//外部存储正常
            File root = Environment.getExternalStorageDirectory();//获取外部存储路径
            File TargetDir = new File(root,super.getPackageName());//在SD卡下创建新的文件夹
            if (!TargetDir.exists()) {
                TargetDir.mkdir();
            }
            try {
                oos = new ObjectOutputStream(new FileOutputStream(new File(TargetDir,"cardSD.TXT")));
                SdCard sdCard = new SdCard("青岛","青岛是一个好地方!");
                oos.writeObject(sdCard);
                Toast.makeText(getApplication(),"写入成功",Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    oos.flush();
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        } else {
            Toast.makeText(getApplication(),"没有找到SD卡",Toast.LENGTH_SHORT).show();
        }

    }
    //读取SD卡内容
    public void ReadFromSD(){
        String state = Environment.getExternalStorageState();//获取外部存储状态
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            File root = Environment.getExternalStorageDirectory();
            File TargetDir = new File(root,super.getPackageName());
            ObjectInputStream ois = null;
            if (TargetDir.exists()) {
                try {
                    ois = new ObjectInputStream(new FileInputStream(new File(TargetDir+"/cardSD.TXT")));
                    SdCard sdCard = (SdCard) ois.readObject();
                    Toast.makeText(SdCardActivity.this,sdCard.getTitle()+" "+sdCard.getContent(),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(),"没有找到SD卡",Toast.LENGTH_SHORT).show();
        }
    }
}

你可能感兴趣的:(Android)