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();
         }
     }

  • 相关阅读:
    Perl的Open函数
    较详细的介绍JNI
    Java多线程单元测试
    sleep函数的简单原理
    Struts与Servlet的冲突
    Ant学习记录
    JDK转码工具
    Throwable
    Entity Framework系列文章导航
    多核时代 .NET Framework 4 中的并行编程1概述
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7222297.html
Copyright © 2011-2022 走看看