zoukankan      html  css  js  c++  java
  • SD卡读取写入

      1 package com.example.administrator.myapplication.activity;
      2 
      3 import android.os.Bundle;
      4 import android.os.Environment;
      5 import android.support.v7.app.AppCompatActivity;
      6 import android.view.View;
      7 import android.widget.Button;
      8 import android.widget.Toast;
      9 
     10 import com.example.administrator.myapplication.R;
     11 import com.example.administrator.myapplication.entity.SDCard;
     12 
     13 import java.io.File;
     14 import java.io.FileInputStream;
     15 import java.io.FileOutputStream;
     16 import java.io.IOException;
     17 import java.io.ObjectInputStream;
     18 import java.io.ObjectOutputStream;
     19 
     20 public class SdCardActivity extends AppCompatActivity {
     21     Button writeBtn;
     22     Button readBtn;
     23     @Override
     24     protected void onCreate(Bundle savedInstanceState) {
     25         super.onCreate(savedInstanceState);
     26         setContentView(R.layout.activity_sd_card);
     27         inno();
     28     }
     29 
     30     private void inno() {
     31         writeBtn = (Button) findViewById(R.id.write);
     32         writeBtn.setOnClickListener(new View.OnClickListener() {
     33             @Override
     34             public void onClick(View v) {
     35                 WriteToSD();
     36             }
     37         });
     38         readBtn = (Button) findViewById(R.id.read);
     39         readBtn.setOnClickListener(new View.OnClickListener() {
     40             @Override
     41             public void onClick(View v) {
     42                 ReadFormSD();
     43             }
     44         });
     45     }
     46 
     47     //保存文件到SD卡
     48     private void WriteToSD(){
     49         ObjectOutputStream oos = null;
     50         //Environment.getExternalStorageState获取外部存储的状态
     51         String state = Environment.getExternalStorageState();
     52         //外部存储正常  如果SD卡存在
     53         if (state.equals(Environment.MEDIA_MOUNTED)){
     54             //获取外部存储的根路径
     55             File root = Environment.getExternalStorageDirectory();
     56             //在SD卡下创建新的文件夹
     57             File TargetDir = new File(root,super.getPackageName());
     58             //若不存在,创建文件夹
     59             if (!TargetDir.exists()){
     60                 TargetDir.mkdir();
     61             }
     62             try {
     63                 oos = new ObjectOutputStream(new FileOutputStream(new File(TargetDir+"SDCard.txt")));
     64                 SDCard sdCard = new SDCard("王倩","是大二的!");
     65                 oos.writeObject(sdCard);
     66                 Toast.makeText(getApplication(),"写入SD卡成功!",Toast.LENGTH_SHORT).show();
     67             } catch (IOException e) {
     68                 e.printStackTrace();
     69             }finally {
     70                 try {
     71                     oos.flush();
     72                     oos.close();
     73                 } catch (IOException e) {
     74                     e.printStackTrace();
     75                 }
     76             }
     77         }else {
     78             Toast.makeText(getApplication(),"没有找到SD卡",Toast.LENGTH_SHORT).show();
     79         }
     80     }
     81 
     82     //从SD卡读取文件
     83     private void ReadFormSD(){
     84         String state = Environment.getExternalStorageState();
     85         if (state.equals(Environment.MEDIA_MOUNTED)){
     86             File root = Environment.getExternalStorageDirectory();
     87             File TargetDir = new File(root,super.getPackageName());
     88             if (TargetDir.exists()){
     89                 try {
     90                     ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(TargetDir+"SDCard.txt")));
     91                     SDCard sdCard = (SDCard) ois.readObject();
     92                     Toast.makeText(SdCardActivity.this,sdCard.getName()+" "+sdCard.getIntroduce(),Toast.LENGTH_SHORT).show();
     93                 } catch (IOException e) {
     94                     e.printStackTrace();
     95                 } catch (ClassNotFoundException e) {
     96                     e.printStackTrace();
     97                 }
     98             }
     99         }
    100     }
    101 }
  • 相关阅读:
    ubutu16.04编译安装apache
    python格式化字符串
    git服务器搭建
    merge into 导致序列跳号
    Apache 强制SSL访问
    pyhton之解析html的表格
    Two modules in a project cannot share the same content root报错解决方案
    hdoj
    hdoj
    QHUOJ
  • 原文地址:https://www.cnblogs.com/xiaolei121/p/5884027.html
Copyright © 2011-2022 走看看