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

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

/**
* 用于在文件里保存程序数据
*
* @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. DB2 v9.7官方下载链接

    http://blog.sina.com.cn/s/blog_8ea8e9d50102w2s6.html

  2. k-密码

    链接:https://www.nowcoder.com/acm/contest/90/K来源:牛客网 题目描述 ZiZi登录各种账号的时候,总是会忘记密码,所以他把密码都记录在一个记事本上.其中第一个 ...

  3. 「转」基于Jmeter和Jenkins搭建性能测试框架

    搭建这个性能测试框架是希望能够让每个人(开发人员.测试人员)都能快速的进行性能测试,而不需要关注性能测试环境搭建过程.因为,往往配置一个性能环境可能需要很长的时间. 1.性能测试流程 该性能测试框架工 ...

  4. Elasticsearch使用java读取数据报错NoNodeAvailableException: None of the configured nodes are available: [127.0.0.1:9300]

    对于这个问题,大部分人出现在这个地方: Client client = new TransportClient(settings).addTransportAddress(new InetSocket ...

  5. [linux]压缩、解压命令

    .tar.gz 和 .tgz 解压:tar zxvf FileName.tar.gz 压缩:tar zcvf FileName.tar.gz DirName tar 解包:tar xvf FileNa ...

  6. CSS 从入门到放弃系列:CSS的引入方式

    css的四种引入方式 内联方式(行间样式) <div style="width:100px;height: 100px; background-color: red"> ...

  7. LCIS最长公共上升子序列

    最长公共上升子序列LCIS,如字面意思,就是在对于两个数列A和B的最长的单调递增的公共子序列. 这道题目是LCS和LIS的综合. 在LIS中,我们通过两重循环枚举当序列以当前位置为结尾时,A序列中当前 ...

  8. 蒟蒻的9个背包的浩大工程(更新中)(无限延期)(太长了不舍删虽然写的lj的一匹)

    所以说这就是一篇写炸的废文!!!! 所以说背包直接看dd大神的就好了,算了瞎写写吧. 0/1背包 有n件物品和一个容量为C的背包.第i件物品的重量是w[i],价值是v[i].求解将哪些物品放入背包可使 ...

  9. 用Python处理邮件,全文完

    http://www.chinaunix.net/old_jh/55/575710.html

  10. java static代码段

    1)java中还有个static代码块的形式,形式为 static {……}.static代码块是类定义的一部分,仅仅在类被初次加载的时候被调用一次,之后再调用不会再加载.那么类什么时候首次被加载呢? ...