zoukankan      html  css  js  c++  java
  • Android--向SD卡读写数据

     // 向SD卡写入数据
         private void writeSDcard(String str) {
             try {
                 // 推断是否存在SD卡
                 if (Environment.getExternalStorageState().equals(
                         Environment.MEDIA_MOUNTED)) {
                     // 获取SD卡的文件夹
                     File sdDire = Environment.getExternalStorageDirectory();
                     FileOutputStream outFileStream = new FileOutputStream(
                             sdDire.getCanonicalPath() + "/test.txt");
                     outFileStream.write(str.getBytes());
                     outFileStream.close();
                     Toast.makeText(this, "数据保存到text.txt文件了", Toast.LENGTH_LONG)
                             .show();
                 }
             } catch (Exception e) {
                 e.printStackTrace();
             }
         }
     
         
         // 从SD卡中读取数据
         private void readSDcard() {
             StringBuffer strsBuffer = new StringBuffer();
             try {
                 // 推断是否存在SD
                 if (Environment.getExternalStorageState().equals(
                         Environment.MEDIA_MOUNTED)) {
                     File file = new File(Environment.getExternalStorageDirectory()
                             .getCanonicalPath() + "/test.txt");
                     // 推断是否存在该文件
                     if (file.exists()) {
                         // 打开文件输入流
                         FileInputStream fileR = new FileInputStream(file);
                         BufferedReader reads = new BufferedReader(
                                 new InputStreamReader(fileR));
                         String st = null;
                         while ((st = reads.readLine()) != null) {
                             strsBuffer.append(st);
                         }
                         fileR.close();
                     } else {
                         Toast.makeText(this, "该文件夹下文件不存在", Toast.LENGTH_LONG).show();
                     }
                 }
             } catch (Exception e) {
                 e.printStackTrace();
             }
             Toast.makeText(this, "读取到的数据是:" + strsBuffer.toString() + "",
                     Toast.LENGTH_LONG).show();
         }
     }

  • 相关阅读:
    day 22 反射,双下方法
    day 21 封装,多态,类的其他属性
    day 20 类与类之间的关系,继承2
    day 19 类的名称空间,组合,派生
    day 18 类,对象
    day 17 re模块
    注意NULL
    SQL_DISTINCT
    重载赋值运算符
    随机序列问题
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7222297.html
Copyright © 2011-2022 走看看