Android 内部存储和Cache简单介绍
来源:互联网 发布:淘宝导航条自定义图片 编辑:程序博客网 时间:2024/06/02 19:29
Internal Storage
你可以直接向内部存储空间写入数据,默认情况下,保存在内部存储的文件是私有化的而其他应用无法进行访问。当你进行了卸载应用,这些文件将被删除。
创建并写入数据到私有化内部存储:
1、调用openFileOutput()并通过参数文件名和运行模式获取FileOutputStream;
2、通过write()写入数据;
3、通过close()关闭IO流。
code snippet:
String string = "data";FileOutputStream fos = null;try { fos = openFileOutput(FILE_NAME, MODE_APPEND); fos.write(string.getBytes());} catch (FileNotFoundException e) { e.printStackTrace();} catch (IOException e) { e.printStackTrace();}finally { if(fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } }}
: FYI
MODE_PRIVATE 运行模式下,会创建文件(如果有相同名字的文件即已经存在这个文件,将会表示这个文件)并且使这个文件对于你的应用来说为私有化。其他模式还有:MODE_APPEND, MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE。MODE_APPEND模式为数据会从你已经写入文件中的数据之后继续写入,而不是覆盖。其他两个模式已经在API17被废弃。
读数据:
1、通过openFileInput()并使用文件名去获取FileIniputStream。
2、使用read()方法去读取bytes;
3、调用close()来关闭IO流。
code snippet:
FileInputStream fis = null;try { fis = openFileInput(FILE_NAME);BufferedReader br = new BufferedReader(new InputStreamReader(fis));String str = null;StringBuffer sb = new StringBuffer(); while ((str = br.readLine()) != null) { sb.append(str);} //TODO} catch (FileNotFoundException e) { e.printStackTrace();} catch (IOException e) { e.printStackTrace();}finally {if(fis != null) {try { fis.close();} catch (IOException e) { e.printStackTrace();} }}
: FYI
如果你想在编译环境使用一个静态的文件,保存这个文件在res/raw文件夹下,你可以使用openRawResource()并通过R.raw.< filename >来获取InputStream,然后读取这个原始文件。比如有如图一个文件:
code snippet:
InputStream fis = null;try { fis = getResources().openRawResource(R.raw.test);BufferedReader br = new BufferedReader(new InputStreamReader(fis));String str = null;StringBuffer sb = new StringBuffer(); while ((str = br.readLine()) != null) { sb.append(str);}// TODO} catch (FileNotFoundException e) { e.printStackTrace();} catch (IOException e) { e.printStackTrace();}finally {if(fis != null) {try { fis.close();} catch (IOException e) { e.printStackTrace();} }}
: FYI
Cache
如果你只是想暂时保留一些数据而不是对它们对持久化存储,你可以使用Cache来短暂性的存储这些数据。当你的设备分配的内存空间不足时,Android会自动删除一些数据而继续保留一定的空间。但是你并不能不主动去做清除动作。按照Google的说法,你应该让你的应用的缓存空间保持剩余,比如1M。当你卸载程序后,缓存会主动被移除。
code snippet:
write:
ObjectOutputStream oos = null;try { File file = new File(MainActivity.this.getCacheDir(), "cache"); if(!file.exists()) { file.createNewFile(); } oos = new ObjectOutputStream(new FileOutputStream(file)); oos.writeObject(new Test("male", "mli"));} catch (IOException e) { e.printStackTrace();}finally { if(oos != null) { try { oos.close(); } catch (IOException e) { e.printStackTrace(); } }}
: FYI
read:
code snippet:
ObjectInputStream ois = null;try { File file = new File(MainActivity.this.getCacheDir(), "cache"); if(!file.exists()) { file.createNewFile(); } ois = new ObjectInputStream(new FileInputStream(file)); Test test = (Test)ois.readObject(); // TODO: ois.close();} catch (IOException e) { e.printStackTrace();} catch (ClassNotFoundException e) { e.printStackTrace();}finally { if(ois != null) { try { ois.close(); } catch (IOException e) { e.printStackTrace(); } }}
:FYI
Mode_private模式写入,数据在重复写的过程中会被覆盖。
remark:
//需要实现Serializable接口,当然,按照规定,这个最好写成外部类
class Test implements Serializable { private static final long serialVersionUID = 2L; String sex; String name; public Test () { } public Test(String sex, String name) { this.sex = sex; this.name = name; } //ObjectOutputStream会按照toString的格式进行存储 @Override public String toString() { return sex + name; }}
很显然,其实上述两种方式的存储方法雷同,只不过存储位置有所改变而已。内部存储存储在/data/data//files目录下,而缓存存储在/data/data//cache目录下。
文章仅供参考,更多,等待探索
- Android 内部存储和Cache简单介绍
- Android 内部存储,外部存储使用范围和介绍
- android基础——内部存储cache
- android 的内存、内部存储和外部存储的理解/cache/system。
- android--内部存储写文件和使用API获取内部存储路径,sharedperferance获取原有的账号密码--简单的登陆(记录原有的账号和密码)--I/0流介绍
- 数据存储详解(二)---->File存储:内部存储 -Cache和外部存储-SD卡
- Android的内部存储和外部存储
- Android 内部存储和外部存储
- Android的内部存储和外部存储
- Android中内部存储和外部存储
- Android内部存储和外部存储
- Android中的内部存储和外部存储
- Android数据存储简单介绍
- Android 存储中的内部存储和外部存储
- Android 数据存储-内部存储和外部存储
- Android 存储中的内部存储和外部存储
- android简单实例---------android的内部存储使用
- android获取外部存储和内部存储路径
- iOS 中单例的使用以及原理
- 优秀程序员的十个习惯
- 更改Windows命令行字体
- solr4.7新建core
- 利用DrawerLayout和触摸事件分发来实现抽屉侧滑业务
- Android 内部存储和Cache简单介绍
- 自定义基类BaseActivity,节省开发步骤
- LeetCode-334.Increasing Triplet Subsequence
- Charged view 0x7f9fb9568c80 was still registered to a window when it was deallocated.
- HDBSQL-命令行模式执行SQL
- easyui的datagrid整合struts2,以及Ajax,实现局部刷新功能,并设置分页的实现---------投票案例
- AspectJ org.aspectj.tools.ajc
- ibatis(0)ibatis 与 mybatis 简述
- PM2 使用介绍