Android开发之sdcard读写数据(源代码分享)
2014-03-21 12:46
387 查看
package com.example.f01_sdcard01; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.os.Environment; public class FileUtiles { // 在向sd卡写入数据时要记得向mainifest清单中增加一个 <uses-permission // android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> // 传递两个参数,一个为文件名,一个为写入的数据 public void fileSave(String fileName, byte[] data) { FileOutputStream fileOutputStream = null; String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { // 创建sdcard目录 File file = new File(Environment.getExternalStorageDirectory() .getAbsolutePath() + "/txt"); // 如果目录不存在,就创建一个文件 if (!file.exists()) { file.mkdirs(); } try { // 将数据写入指定文件中 fileOutputStream = new FileOutputStream( new File(file, fileName)); try { fileOutputStream.write(data, 0, data.length); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } }public String readSdcard(String fileName){ //读取指定文件的数据
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { File file = new File(Environment.getExternalStorageDirectory() .getAbsolutePath() + "/txt/"); // 如果目录不存在,就创建一个文件 if (file.exists()) { File file2=new File(file, fileName); InputStream inputStream=null; ByteArrayOutputStream arrayOutputStream=new ByteArrayOutputStream(); try { inputStream = new FileInputStream(file2); byte[] data=new byte[1024]; int len=0; try { while((len=inputStream.read(data))!=-1){ arrayOutputStream.write(data, 0, len); } return new String(arrayOutputStream.toByteArray()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(inputStream!=null){ try { inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } return null; }
相关文章推荐
- android开发步步为营之87:从sdcard或者data文件夹下读写数据
- Android开发之异步任务加载网络图片并存储在sdcard中(源代码分享)
- Android数据存储(2)向机身内存(Internal Storage)和SDcard(External Storage)读写数据实例
- Android开发之通过CursorAdapter读取数据(源代码分享)
- Android开发中获取SDcard、手机内存空间大小的源代码
- Android App开发记录—sd Card 读写
- Android开发之ContentProvider结合LoaderManager加载数据(图文源代码分享)
- Android数据存储(2)向机身内存(Internal Storage)和SDcard(External Storage)读写数据实例
- Android开发之通过接口回调机制加载数据(源代码分享)
- Android开发之Fragment与Activity的数据交互通过回调机制实现(源代码分享)
- [120_移动开发Android]006_android开发之数据存储之sdcard访问
- Android中SdCard的数据读写
- 在android开发中正确读写sdcard文件的方法
- Android开发之Fragment与Activity的数据交互通过回调机制实现(源代码分享)
- android 开发-文件存储之读写sdcard
- Android数据存储(2)向机身内存(Internal Storage)和SDcard(External Storage)读写数据实例
- Android中SdCard的数据读写
- Android开发之Service与Activity数据交互(源代码分享)
- Android开发中正确读写sdcard文件的方法
- 【Android游戏开发之十】(优化处理)详细剖析Android Traceview效率检视工具,分析程序运行速度!并讲解两种创建SDcard方式!