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         ObjectInputStream ois = null;
     85         String state = Environment.getExternalStorageState();
     86         if (state.equals(Environment.MEDIA_MOUNTED)){
     87             File root = Environment.getExternalStorageDirectory();
     88             File TargetDir = new File(root,super.getPackageName());
     89             if (TargetDir.exists()){
     90                 try {
     91                     ois = new ObjectInputStream(new FileInputStream(new File(TargetDir+"SDCard.txt")));
     92                     SDCard sdCard = (SDCard) ois.readObject();
     93                     Toast.makeText(SdCardActivity.this,sdCard.getName()+" "+sdCard.getIntroduce(),Toast.LENGTH_SHORT).show();
     94                 } catch (IOException e) {
     95                     e.printStackTrace();
     96                 } catch (ClassNotFoundException e) {
     97                     e.printStackTrace();
     98                 }finally {
     99                     try {
    100                         ois.close();
    101                     } catch (IOException e) {
    102                         e.printStackTrace();
    103                     }
    104                 }
    105             }
    106         }
    107     }
    108 }
  • 相关阅读:
    Excel操作基本方法 服务器端不用安装Office工具
    使用IrisSkin2给软件"换肤"
    手机进行GPRS连接的代码(C#.NET开发)
    C# DataGridView 倒出word
    win2003优化大全 (转载)
    c# Invoke和BeginInvoke 区别
    关于sqlite3使用top的一些规则总结
    C#拷贝文件和复制文件夹实例代码 C#拷贝文件
    c# FileSystemWatcherc# FileSystemWatcher控件的使用方法控件的使用方法
    所见即所得富文本编辑器实现原理 转
  • 原文地址:https://www.cnblogs.com/xiaolei121/p/6026317.html
Copyright © 2011-2022 走看看