【Android进阶】怎样使用文件来保存程序中的数据
2023-09-11 14:20:59 时间
在程序中。有非常多保存和获取数据的方法,本篇文章,主要介绍使用文件系统对程序中的数据进行保存和读取的操作
我直接写了一个帮助类,进行文件的写入和读取操作
/** * 用于在文件里保存程序数据 * * @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 Security] 静态分析Android程序——smali文件解析
- [Android Pro] 查看 keystore文件的签名信息 和 检查apk文件中的签名信息
- 在Android Studio中使用shareSDK进行社会化分享(图文教程)
- android eclipse写layout文件失效问题解决
- Android 开发之旅:深入分析布局文件&又是“Hello World!”
- 《android开发进阶从小工到专家》读书笔记--网络框架的设计与实现
- 《Android源码设计模式》--Builder模式
- Cocos移植到Android的一些问题-中文乱码问题
- EasyPlayer Android基于ffmpeg实现播放(RTSP/RTMP/HTTP/HLS)同步录像功能
- Android开发学习---使用Intelij idea 13.1 进行android 开发
- Android开发学习---android下的数据持久化,保存数据到rom文件,android_data目录下文件访问的权限控制
- android: minSdkVersion、targetSdkVersion、CompileSdkVersion三个api版本号的区别
- Android GIS开发系列-- 入门季(13)Gdal简单写个shp文件
- 《深入理解Android 卷III》— Android 书籍
- Android Listview设置每条信息的间距
- Android Studio 详解build.gradle文件
- android 9.0 10.0 framework层 Camera旋转摄像头方向
- 【Android 逆向】Android 进程注入工具开发 ( 远程进程注入动态库文件操作 | 注入动态库 加载 业务动态库 | 业务动态库启动 | pthread_create 线程开发 )
- 【Android 逆向】Android 系统文件分析 ( 外部存储设备文件 | sbin 命令程序目录 | dev 字符设备目录 )
- 【Android 热修复】热修复原理 ( Dex 文件拷贝后续操作 | 外部存储空间权限申请 | 执行效果验证 | 源码资源 )
- 【Android布局】在程序中设置android:gravity 和 android:layout_Gravity属性
- 理解Android Framework
- 【Android布局】在程序中设置android:gravity 和 android:layout_Gravity属性
- ubuntu下编译android jni到so库的mk文件配置
- android电池充电动画,Android 开机充电图标和充电动画效果
- 迁移到Android后android.support.v4.content.FileProvider运行崩溃