在程序,有许多方法来存储和检索数据,本文,它描述了如何使用文件系统来保存数据编程和读取操作

我直接写了一个帮助类,进行文件的写入和读取操作

/**
* 用于在文件里保存程序数据
*
* @author zhaokaiqiang
*
*/
public class FileHelper { private static final String TAG = "FileHelper";
private Context mContext; FileHelper(Context _mContext) {
mContext = _mContext;
} // 在手机本地硬盘中保存信息
public void save(String fileName, String content) { FileOutputStream fileOutputStream = null;
try {
fileOutputStream = mContext.openFileOutput(fileName,
Context.MODE_PRIVATE);
fileOutputStream.write(content.getBytes()); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try { if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} // 读取手机硬盘中保存的文件
public void read(String fileName) {
FileInputStream fileInputStream = null;
try {
fileInputStream = mContext.openFileInput(fileName);
int len = 0;
byte[] buffer = new byte[1024];
ByteArrayOutputStream byteArrayInputStream = new ByteArrayOutputStream();
while ((len = fileInputStream.read(buffer)) != -1) {
byteArrayInputStream.write(buffer, 0, len);
}
String string = new String(byteArrayInputStream.toByteArray());
Log.d(TAG, string);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
}

注意:使用写入操作的时候。写入的内容会将上次写入的内容进行覆盖

写入的文件保存在/data/data/package name/files文件夹下,使用DDMS能够进行查看

例如以下图所看到的:

使用DDMS将文件导出。就可以查看内容

上面这些是将数据写入到我们的手机自带的存储空间里,假设想写入我们的SDCard,那么应该怎么做呢?

以下的写入到SDCard的操作

// save infomation in the SDCard
public boolean saveToSDCard(String fileName, String content) { // judge weather the SDCard exits,and can be read and written
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
return false;
} FileOutputStream fileOutputStream = null;
File file = new File(Environment.getExternalStorageDirectory(),
fileName);
try {
fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(content.getBytes());
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try { if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}

以下是读取位于SDCard根文件夹下文件的操作方法

// read the file in the SDCard
public String readFromSD(String fileName) {
FileInputStream fileInputStream = null;
File file = new File(Environment.getExternalStorageDirectory(),
fileName);
try {
fileInputStream = new FileInputStream(file);
int len = 0;
byte[] buffer = new byte[1024];
ByteArrayOutputStream byteArrayInputStream = new ByteArrayOutputStream();
while ((len = fileInputStream.read(buffer)) != -1) {
byteArrayInputStream.write(buffer, 0, len);
}
String string = new String(byteArrayInputStream.toByteArray());
return string;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} return null;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

最新文章

  1. C#GDI+基础(三)画刷详解
  2. Android--Apache HttpClient
  3. 抢滩登陆游戏android源码
  4. 从WeUI学习到的知识点
  5. FreeMarker笔记 第四章 其它
  6. iOS最新上线流程+续费 2015-7-20更新
  7. 网站加载有商务通、商桥,定义js函数触发快商通代码
  8. python连接redis002
  9. 虚拟机有QQ消息时宿主机自动弹窗提示
  10. AngularJS 1.x系列:AngularJS服务-Service、Factory、Provider、Value及Constant(5)
  11. 微信公众号_订阅号_微信JS-SDK网页开发
  12. SpringMVC 框架完成图片上传到项目路径操作
  13. mybatis多数据源切换
  14. nfs的时间问题,影响编译
  15. JAVA-JSP内置对象之pageContext对象取得不同范围属性
  16. C#注册表读写完整操作类
  17. 用cmd导入oracle的.dmp文件和修改oracle管理员密码
  18. Ansible之迭代、模板
  19. Java中HashMap 初始化时容量(参数)如何设置合适?
  20. CF438 The Child and Sequence

热门文章

  1. SAP ABAP编程 字符串加密-MD5_CALCULATE_HASH_FOR_CHAR
  2. Android String与十六进制数互转
  3. css3 border img 边框图片
  4. 【27.77%】【BZOJ 4066】简单题
  5. ant使用ssh和linux交互 如:上传文件
  6. 伸展树(splay tree)
  7. PatentTips - Scheduling compute kernel workgroups to heterogeneous processors based on historical processor execution times and utilizations
  8. iPad和iPhone开发的异同
  9. 【codeforces 768B】Code For 1
  10. WPF 针对数据源某个属性进行排序