在程序中。有非常多保存和获取数据的方法,本篇文章,主要介绍使用文件系统对程序中的数据进行保存和读取的操作

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

/**
* 用于在文件里保存程序数据
*
* @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;
}

【Android进阶】怎样使用文件来保存程序中的数据的更多相关文章

  1. 【Android先进】如何使用数据文件来保存程序

    在程序,有许多方法来存储和检索数据,本文,它描述了如何使用文件系统来保存数据编程和读取操作 我直接写了一个帮助类,进行文件的写入和读取操作 /** * 用于在文件里保存程序数据 * * @author ...

  2. c#保存datagridview中的数据时报错 “动态SQL生成失败。找不到关键信息”

    ilovejinglei 原文 C#中保存datagridview中的数据时报错"动态SQL生成失败.找不到关键信息" 问题描述     相关代码 using System; us ...

  3. android:http下载文件并保存到本地或SD卡

    想把文件保存到SD卡中,一定要知道SD卡的路径,获取SD卡路径: Environment.getExternalStorageDirectory() 另外,在保存之前要判断SD卡是否已经安装好,并且可 ...

  4. 转-Android 之 使用File类在SD卡中读取数据文件

    如果需要在程序中使用sdcard进行数据的存储,那么需要在AndroidMainfset.xml文件中 进行权限的配置: Java代码:   <!-- 在sd中创建和删除文件的权限 --> ...

  5. Java 从资源文件(.properties)中读取数据

    在Java工程目录src下,创建一个后缀为.properties的文件,例如db.properties 文件中的内容如下(键=值): name=mk age=123 address=China 在程序 ...

  6. SAP SMARTFORMS-基于内存方式在report程序中传递数据到smartforms显示

    一.准备工作 1.新建include程序 1> include程序名字:ZDD_INC_0104 2> ZDD_INC_0104  程序中的内容为 2.使用T-CODE :SE11新建两个 ...

  7. 如何在原生微信小程序中实现数据双向绑定

    官网:https://qiu8310.github.io/minapp/ 作者:Mora 在原生小程序开发中,数据流是单向的,无法双向绑定,但是要实现双向绑定的功能还是蛮简单的! 下文要讲的是小程序框 ...

  8. android 通过httpclient下载文件并保存

    代码:(主要针对图片.gif下载无问题) /** * 下载网络文件 * @param url 请求的文件链接 * @param IsMD5Name 是否MD5加密URL来命名文件名 * @param ...

  9. 【我的Android进阶之旅】如何去除ListView中Header View、Footer View中的分割线

    最近的项目中给ListView 加入了一个Header View之后,发现Header View的下方也有了分割线,很难看,UI要求将Header View的分割器去掉,好吧.现在就来说一说如何如何去 ...

随机推荐

  1. fmod()函数和modf()函数

    最近从博客上看到了一个fmod函数,结果又蹦出来一个modf函数 fmod()函数: 头文件:#include<math.h> C库函数... fmod()用来对浮点数进行取模(求余),原 ...

  2. Windows下python的第三方库的安装

    D:\Python27\Scripts\pip.exe install beautifulsoup4

  3. CF 996B World Cup 【找规律/模拟】

    CF [题意]:圆形球场有n个门,Allen想要进去看比赛.Allen采取以下方案进入球场:开始Allen站在第一个门,如果当前门前面有人Allen会花费单位时间走到下一个门,如果没人Allen从这个 ...

  4. 【bzoj1335】Radio Transmission

    给你一个字符串,它是由某个字符串不断自我连接形成的.但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 因为这个字符串是不断自匹配形成的我们可以很容易的想到,除了第一个字符串外,从第二个字符串 ...

  5. 详解jQuery的选择器

    1.基本选择器 基本选择器是jQuery中最常用的选择器,也是最简单的选择器,它通过元素id.class和标签名等来查找DOM对象.在网页中,每个id名称只能使用一次,class允许重复使用. ♠ # ...

  6. TDD开发案例

    前段时间把一个界面框架完成了,今天基于这个框架开发一个小模块,在这里把这个模块设计的全过程记录下来,希望大家讨论并指正. 一.起因 公司交给我一个任务,为测试员写一个手机模拟界面,以方便她们的手机短信 ...

  7. [BZOJ 2817] 波浪

    Link: BZOJ 2817 传送门 Solution: 算是比较神的DP了吧, 首先这个绝对值处理起来很难受,肯定要想办法去掉 于是想到从小到大插入的方式,便不存在绝对值的问题了 插入一个数只有5 ...

  8. 【R笔记】glm函数报错原因及解析

    R语言glm函数学习:  [转载时请注明来源]:http://www.cnblogs.com/runner-ljt/ Ljt 作为一个初学者,水平有限,欢迎交流指正. glm函数介绍: glm(for ...

  9. Spark小问题合集

    1)在win7下使用spark shell运行spark程序,通过以下形式读取文件时 sc.sequenceFile[Int,String]("./sparkF") 偶尔会出现“I ...

  10. 收藏起来,史上最全的 MySQL 高性能优化实战总结!

    转自:https://mp.weixin.qq.com/s/sRsJzFO9dPtKhovJNWN3Dg 一.前言 MySQL 对于很多 Linux 从业者而言,是一个非常棘手的问题,多数情况都是因为 ...