【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进阶】怎样使用文件来保存程序中的数据
在程序中.有非常多保存和获取数据的方法,本篇文章,主要介绍使用文件系统对程序中的数据进行保存和读取的操作 我直接写了一个帮助类,进行文件的写入和读取操作 /** * 用于在文件里保存程序数据 * * ...
- Android系统编程入门系列之应用数据文件化保存
应用中关于数据的持久化保存,不管是简单的SharedPreferences还是数据库SQLiteDatabase,本质上都是将数据保存到系统的某种类型的文件中.因此可以直接使用java.io.File ...
- 我的Android 4 学习系列之文件、保存状态和首选项
目录 使用Shared Preference 保留简单的应用程序数据 保存回话间的Activity实例数据 管理应用程序首选项和创建Preference Screen 保存并加载文件以及管理本地文件系 ...
- android中使用Http下载文件并保存到本地SD卡
1.AndroidMainfest.xml中设置权限 <uses-permission android:name="android.permission.INTERNET"& ...
- Java读取oracle数据库中blob字段数据文件保存到本地文件(转载)
转自:https://www.cnblogs.com/forever2698/p/4747349.html package com.bo.test; import java.io.FileOutput ...
- 【基础】Oracle 表空间和数据文件
多个表空间的优势:1.能够将数据字典与用户数据分离出来,避免由于字典对象和用户对象保存在同一个数据文件中而产生的I/O冲突2.能够将回退数据与用户数据分离出来,避免由于硬盘损坏而导致永久性的数据丢失3 ...
- pyhton读入Excel和csv数据文件
pyhton读入Excel和csv数据文件#file 数据文件的输入输出操作(主要包括Excel表格和csv表格文件)import pandas as pd #pyhton读入数据必须要导入panda ...
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...
- 转-Android 之 使用File类在SD卡中读取数据文件
如果需要在程序中使用sdcard进行数据的存储,那么需要在AndroidMainfset.xml文件中 进行权限的配置: Java代码: <!-- 在sd中创建和删除文件的权限 --> ...
随机推荐
- Spinlock implementation in ARM architecture
Spinlock implementation in ARM architecture SEV and WFE are the main instructions used for impleme ...
- js进阶js中支持正则的四个常用字符串函数(search march replace split)
js进阶js中支持正则的四个常用字符串函数(search march replace split) 一.总结 代码中详细四个函数的用法 search march replace split 二.js进 ...
- Session or Cookie?是否需要用Tomcat等Web容器的Session
Cookie是HTTP协议标准下的存储用户信息的工具,浏览器把用户信息存放到本地的文本文件中. Session是基于Cookie实现的. 2011年4月,武汉群硕面试的时候(实习生),面试官也问过这个 ...
- Use Word 2010's Navigation Pane to quickly reorganize documents
Use Word 2010's Navigation Pane to quickly reorganize documents http://www.techrepublic.com/blog/mic ...
- Java环境搭建若干问题
0.总体说明 本次搭建环境,为了偷懒,使用的是,阿里云镜像. 自带了Nginx.Tomcat.JDK等. 比较坑爹的是,虽然镜像带了很多安装好的软件,但是也有各种问题,比如它修改了tomc ...
- 微服务学习笔记(1)——使用MagicOnion实现gRPC
原文:微服务学习笔记(1)--使用MagicOnion实现gRPC 1.什么是gRPC 官方文档:https://grpc.io/docs/guides/index.html 2.什么是MagicOn ...
- NOIP模拟 Work - 二分 + 树状数组 / ???
题目分析 如果没有最后的注意事项,此题就是二分裸题.有了注意事项,会有两种思路: 在线:二分天数t,并在主席树上求1~t天中大于d(浪费的时间)的时间之和以及数量,答案即为:sum - d * cnt ...
- 【t012】整理书架
Time Limit: 1 second Memory Limit: 32 MB [问题描述] 小明是一个非常喜欢读书的孩子,他有一个特别的书架,书架上摆放着他买的新书.当他决定要阅读某本图书时,他就 ...
- 【t003】string
Time Limit: 1 second Memory Limit: 50 MB [问题描述] 设有字符串X,我们称在X的头尾及中间插入任意多个空格后构成的新字符串为X的扩展串,如字符串X为" ...
- 巧用redis位图存储亿级数据与访问 - 简书
原文:巧用redis位图存储亿级数据与访问 - 简书 业务背景 现有一个业务需求,需要从一批很大的用户活跃数据(2亿+)中判断用户是否是活跃用户.由于此数据是基于用户的各种行为日志清洗才能得到,数据部 ...