博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android--向SD卡读写数据
阅读量:6966 次
发布时间:2019-06-27

本文共 1967 字,大约阅读时间需要 6 分钟。

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

转载地址:http://ixisl.baihongyu.com/

你可能感兴趣的文章
URAL 1913 Titan Ruins: Old Generators Are Fine Too
查看>>
Horizon Workspace 快速部署指南三(配置Workspace数据模块)
查看>>
C# 添加Excel水印
查看>>
我的友情链接
查看>>
mysql导出和导入
查看>>
IT软件创业之 -- 电脑设备买过来都是钱,卖出去都是废铁
查看>>
debian的“chkconfig”和“service”
查看>>
敏捷开发
查看>>
"无法启动应用程序,工作组信息文件丢失,或是已被其他用户已独占方式打开"解决办法...
查看>>
MYSQL远程连接授权
查看>>
安装hadoop图文
查看>>
Nginx的连接处理方法
查看>>
22个开源的PHP框架
查看>>
进桌面点右键就提示内存不能读,再点确定后就自动注销。
查看>>
New Features in Xcode 6
查看>>
反射应用和集合按照某个字段排序
查看>>
NFS基于linux用户和UINX用户的文件共享
查看>>
Perl UTF-8字符开关
查看>>
JAVASE 增强
查看>>
iptables常用选项及应用实例操作
查看>>