【Android进阶】怎样使用文件来保存程序中的数据
在程序中。有非常多保存和获取数据的方法,本篇文章,主要介绍使用文件系统对程序中的数据进行保存和读取的操作
我直接写了一个帮助类,进行文件的写入和读取操作
/**
* 用于在文件里保存程序数据
*
* @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进阶】怎样使用文件来保存程序中的数据的更多相关文章
- 【Android先进】如何使用数据文件来保存程序
在程序,有许多方法来存储和检索数据,本文,它描述了如何使用文件系统来保存数据编程和读取操作 我直接写了一个帮助类,进行文件的写入和读取操作 /** * 用于在文件里保存程序数据 * * @author ...
- c#保存datagridview中的数据时报错 “动态SQL生成失败。找不到关键信息”
ilovejinglei 原文 C#中保存datagridview中的数据时报错"动态SQL生成失败.找不到关键信息" 问题描述 相关代码 using System; us ...
- android:http下载文件并保存到本地或SD卡
想把文件保存到SD卡中,一定要知道SD卡的路径,获取SD卡路径: Environment.getExternalStorageDirectory() 另外,在保存之前要判断SD卡是否已经安装好,并且可 ...
- 转-Android 之 使用File类在SD卡中读取数据文件
如果需要在程序中使用sdcard进行数据的存储,那么需要在AndroidMainfset.xml文件中 进行权限的配置: Java代码: <!-- 在sd中创建和删除文件的权限 --> ...
- Java 从资源文件(.properties)中读取数据
在Java工程目录src下,创建一个后缀为.properties的文件,例如db.properties 文件中的内容如下(键=值): name=mk age=123 address=China 在程序 ...
- SAP SMARTFORMS-基于内存方式在report程序中传递数据到smartforms显示
一.准备工作 1.新建include程序 1> include程序名字:ZDD_INC_0104 2> ZDD_INC_0104 程序中的内容为 2.使用T-CODE :SE11新建两个 ...
- 如何在原生微信小程序中实现数据双向绑定
官网:https://qiu8310.github.io/minapp/ 作者:Mora 在原生小程序开发中,数据流是单向的,无法双向绑定,但是要实现双向绑定的功能还是蛮简单的! 下文要讲的是小程序框 ...
- android 通过httpclient下载文件并保存
代码:(主要针对图片.gif下载无问题) /** * 下载网络文件 * @param url 请求的文件链接 * @param IsMD5Name 是否MD5加密URL来命名文件名 * @param ...
- 【我的Android进阶之旅】如何去除ListView中Header View、Footer View中的分割线
最近的项目中给ListView 加入了一个Header View之后,发现Header View的下方也有了分割线,很难看,UI要求将Header View的分割器去掉,好吧.现在就来说一说如何如何去 ...
随机推荐
- 山东省第八届省赛 A:Return of the Nim(尼姆+威佐夫)
Problem Description Sherlock and Watson are playing the following modified version of Nim game: Ther ...
- HDU 2824.The Euler function-筛选法求欧拉函数
欧拉函数: φ(n)=n*(1-1/p1)(1-1/p2)....(1-1/pk),其中p1.p2…pk为n的所有素因子.比如:φ(12)=12*(1-1/2)(1-1/3)=4.可以用类似求素数的筛 ...
- (6)python 循环控制
一.if语句 1.条件执行和if语句 2.else子句 3.elif子句 多个检查条件时 4.嵌套代码 5.空语句 使用pass占位 6.断言 关键字 assert 判断表达式前加上这个关键字表示, ...
- 暴力 【p4092】[HEOI2016/TJOI2016]树
Description 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下两种操作: 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其 ...
- hihocoder Counting Islands II(并查集)
Counting Islands II 描述 Country H is going to carry out a huge artificial islands project. The projec ...
- Linux命令之ps
ps [选项] 报告当前进程状态.ps显示有关选择的活动进程的信息.如果要重复更新选择和显示的信息,请使用top.ps命令可以搭配kill随时中断.删除不必要的程序.ps命令是最基本同时也是非常强大的 ...
- 【推导】【贪心】Codeforces Round #402 (Div. 2) E. Bitwise Formula
按位考虑,每个变量最终的赋值要么是必为0,要么必为1,要么和所选定的数相同,记为2,要么和所选定的数相反,记为3,一共就这四种情况. 可以预处理出来一个真值表,然后从前往后推导出每个变量的赋值. 然后 ...
- 【块状树】bzoj3731 Gty的超级妹子树
带 加点 删边的块状树. 加点在 bzoj3720 说过. 删边其实就是块顶打标记,记录其属于哪棵树,防止在dfs搜集答案时跑到别的树上. 然后暴力把所在块拆开. 好像用邻接表存图,直接在vector ...
- 【树上莫队】【带修莫队】【权值分块】bzoj1146 [CTSC2008]网络管理Network
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using ...
- 【暴力】【推导】bzoj1088 [SCOI2005]扫雷Mine
考虑右侧的一个格子是否放雷,只可能对其左侧的三个格子造成影响. 也就是说,若左侧一个格子旁的两个格子已经放了雷,对第三个格子也就唯一确定了. 因此只枚举前两个格子是否放雷,剩下的暴力判断是否合法即可. ...