【Android先进】如何使用数据文件来保存程序
2015-07-04 18:16
656 查看
在程序,有许多方法来存储和检索数据,本文,它描述了如何使用文件系统来保存数据编程和读取操作
我直接写了一个帮助类,进行文件的写入和读取操作
注意:使用写入操作的时候。写入的内容会将上次写入的内容进行覆盖
写入的文件保存在/data/data/package name/files文件夹下,使用DDMS能够进行查看
例如以下图所看到的:
使用DDMS将文件导出。就可以查看内容
上面这些是将数据写入到我们的手机自带的存储空间里,假设想写入我们的SDCard,那么应该怎么做呢?
以下的写入到SDCard的操作
以下是读取位于SDCard根文件夹下文件的操作方法
我直接写了一个帮助类,进行文件的写入和读取操作
/** * 用于在文件里保存程序数据 * * @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杀进程方法
- adb脚本输出android调试信息
- android 随着认识的去除率EditText(它配备了防抖效果)
- Android开发中 更新API的问题
- Android物业动画研究(Property Animation)彻底解决具体解释
- Android正则表达式验证手机号号段和位数
- android平台收发邮件教程
- [Android L]SEAndroid增强Androd安全性背景概要及带来的影响
- 记录几个Android上利用Camera传输视频流的开源例子
- Android学习之加圆点引导页的实现
- openGL ES Android 三角形绘制
- Android 一键清理、内存清理功能实现
- Android 实现记住用户名和密码的功能
- Android下强制关闭其他程序
- android 应用强制停止
- Android拖动和缩放图片
- android定义dialog
- android imageButton 透明图片
- Android开发(三十)——ScrollView中ListView的高度自动适应
- android短信验证