原文出处:http://blog.csdn.net/yihuiworld/article/details/46429577
Android数据存储之内存读写的两大内存
1、手机内存数据读写
getFileDir( ) : 得到当前app在手机内存存储数据的位置 /data/data/当前app包名/files
getCacheDir( ) : 得到当前app在手机内存存储数据的位置/data/data/当前app包名/cache
openFileInput(String name) : 直接得到/data/data/当前app包名/files/name文件的输入流
openFileOutput(String name,int mode) : 直接得到/data/data/当前app包名/files/name文件的输出流,mode为写入文件时的权限
2、sdcard数据读写
Environment.getExternalStorageDirectory( ) : 得到当前app所在手机的sdcard位置/storage/sdcard
Environment.getExternalStoragePublicDirectory(String type) : 得到当前app所在手机的sdcard位置下的公共子文件夹/storage/sdcard/....
Android数据存储之内存读写应用实例
布局文件 activity_main.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
-
- <TextView
- android:id="@+id/tv_main_memerysize"
- android:layout_width="match_parent"
- android:layout_height="80dip"
- android:textSize="20sp"
- android:textColor="#6666ff"
- android:text="内存大小"/>
-
- <TextView
- android:id="@+id/tv_main_sdcard"
- android:layout_width="match_parent"
- android:layout_height="40dip"
- android:textSize="20sp"
- android:textColor="#ff6666"
- android:text="SDCard存在否"/>
- <TextView
- android:id="@+id/tv_main_sdcardsize"
- android:layout_width="match_parent"
- android:layout_height="80dip"
- android:textSize="20sp"
- android:textColor="#ff6666"
- android:text="SDCard内存大小"/>
-
- <EditText
- android:id="@+id/et_main_content"
- android:layout_width="match_parent"
- android:layout_height="0dip"
- android:layout_weight="1"
- android:textSize="20sp"
- android:text="测试写入内存:\n将这里的文本信息保存到内存文件 testmemeryio.txt"/>
-
- <Button
- android:id="@+id/btn_main_writememery"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="写入内存"/>
- <Button
- android:id="@+id/btn_main_writesdcard"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="写入sdcard"/>
-
- </LinearLayout>
示例代码 MainActivity.Java
注意:因为要读写SDCard,所以在项目里面的AndroidManifest.xml功能清单文件里面添加读写SDCard的权限
- ......
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="19" />
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- ......
运行效果:
![](https://img-blog.csdn.net/20150610101813591)
在File Explorer下可以看到 /data/data/com.yihui.iomemery/files/testmemeryio.txt文件,且写入的内容就是UI上面文本框里面的内容
/storage/sdcard/m.txt文件,且写入的内容也是UI上面文本框里面的内容
Android数据存储之读写模式(读写权限)
Android是基于Linux操作系统的,所以Android的文件访问权限与Linux系统的文件访问权限是一致的,具体参考下图说明
![](https://img-blog.csdn.net/20150610101738309)
1、Android数据访问权限实现方式
Context类的public abstract FileOutputStream openFileOutput(String name, int mode)
2、其中mode就是文件访问权限模式,主要4种模式
Context.MODE_PRIVATE:私有模式(默认模式),只能被应用本身和同一群组的人访问;写入的内容覆盖原文件内容
Context.MODE_APPEND: 追加模式也是私有模式,只能被应用本身和同一群组的人访问;如果文件存在就追加内容,如果文件不存在就新建文件并写入内容
Context.MODE_WORLD_READABLE: 所有人可读权限
Context.MODE_WORLD_WRITEABLE
:
所有人可写权限
也可通过多个组合同时拥有多个读写权限,Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE表示所有人可读+可写
http://blog.csdn.net/yihuiworld