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

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

/**
* 用于在文件里保存程序数据
*
* @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进阶】怎样使用文件来保存程序中的数据

    在程序中.有非常多保存和获取数据的方法,本篇文章,主要介绍使用文件系统对程序中的数据进行保存和读取的操作 我直接写了一个帮助类,进行文件的写入和读取操作 /** * 用于在文件里保存程序数据 * * ...

  2. Android系统编程入门系列之应用数据文件化保存

    应用中关于数据的持久化保存,不管是简单的SharedPreferences还是数据库SQLiteDatabase,本质上都是将数据保存到系统的某种类型的文件中.因此可以直接使用java.io.File ...

  3. 我的Android 4 学习系列之文件、保存状态和首选项

    目录 使用Shared Preference 保留简单的应用程序数据 保存回话间的Activity实例数据 管理应用程序首选项和创建Preference Screen 保存并加载文件以及管理本地文件系 ...

  4. android中使用Http下载文件并保存到本地SD卡

    1.AndroidMainfest.xml中设置权限 <uses-permission android:name="android.permission.INTERNET"& ...

  5. Java读取oracle数据库中blob字段数据文件保存到本地文件(转载)

    转自:https://www.cnblogs.com/forever2698/p/4747349.html package com.bo.test; import java.io.FileOutput ...

  6. 【基础】Oracle 表空间和数据文件

    多个表空间的优势:1.能够将数据字典与用户数据分离出来,避免由于字典对象和用户对象保存在同一个数据文件中而产生的I/O冲突2.能够将回退数据与用户数据分离出来,避免由于硬盘损坏而导致永久性的数据丢失3 ...

  7. pyhton读入Excel和csv数据文件

    pyhton读入Excel和csv数据文件#file 数据文件的输入输出操作(主要包括Excel表格和csv表格文件)import pandas as pd #pyhton读入数据必须要导入panda ...

  8. 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)

    1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...

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

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

随机推荐

  1. vim :g

    https://stackoverflow.com/questions/1946738/vim-how-to-delete-every-second-row An elegant (and effic ...

  2. 终端复用工具tmux的使用

    tmux的作用在于终端复用. 1. 在server上启动一个bash.并在里面执行tmux 2. 通过ssh远程登录server,执行tmux attach,就会切换到server上的那个bash中, ...

  3. php实现求对称二叉树(先写思路,谋而后动)

    php实现求对称二叉树(先写思路,谋而后动) 一.总结 1.先写思路,谋而后动 二.php实现求对称二叉树 题目描述: 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的 ...

  4. WP8.1开发:简单的天气预报应用

    今天小梦给大家分享一个简单的天气预报应用源码:调用的是百度API.整个应用都没有什么难点.只是一个简单的网络请求和json数据处理.在WP8.1有小娜的情况下,天气预报应用还有意义吗?我认为还是有点意 ...

  5. [GraphQL] Create an Input Object Type for Complex Mutations

    When we have certain mutations that require more complex input parameters, we can leverage the Input ...

  6. Kolya and Tandem Repeat

     Kolya and Tandem Repeat time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  7. 【codeforces 760B】Frodo and pillows

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. Double prefix overrides to provide 16-bit operand size in a 32/64 operating mode

    A processor supports an operating mode in which the default address size is greater than 32 bits and ...

  9. 【vs调试】PDB 文件:每个开发人员都必须知道的

    [vs调试]PDB文件:每个开发人员都必须知道的 GDB:The GNU Project Debugger, 将会包含代码中符号(自定义变量, 数据类型), 还有函数调用或类引用的关联性, 有了pdb ...

  10. 进入Erlang的世界

    http://erlang.group.iteye.com/group/wiki/1407-to-enter-the-world-of-erlang 进入Erlang的世界 作为程序员,我们曾经闻听很 ...