说到Android中的存储方式,大家应该都会想到手机的文件存储,sd卡存储,sp存储与数据库存储,这些其实我之前已经写过一次,但是当时编辑的比较凌乱,看起来不太方便,所以现在重新写四大存储方式并进行讲解,此本讲的就是文件的存储与读取了,闲话少说,Code送上。
UI Map:
MainActivity Code:
MainActivity xml Code:
<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" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/main_edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="在此输入要保存的信息" /> <Button android:id="@+id/save_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="文件保存" /> </LinearLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/load_one" android:text="读取文件的数据" android:gravity="center" /> <TextView android:id="@+id/main_content" android:text="读取数据展示区" android:gravity="center" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp" /></LinearLayout>end map: 1.这里我们输入要保存的东西,然后点击保存
2.通过DDMS导出数据,进行数据查询
3.查看导出的数据(我们发现数据是没有问题的哦,在此写入成功)
4.我们现在使用的就是数据读取了(显示区域也是没有问题) 
使用方式: 1.使用openFileOutput存储,openFileInput读取 2.输入的时候为写入,所以使用的是Output,输出的时候为读取,使用的Input 3.对应的使用BufferedWrite与BufferedRead,之后传入上文的FileOutputStream与FileInputStream 4.BufferedRead因为要读取,所有记得StringBuilder的容器 5.记得close,避免内存泄漏
注意:
记得添加读写的权限
<uses-permission android:name="android.permission.WRITE_PROFILE"/> <uses-permission android:name="android.permission.READ_PROFILE"/>文件存储有时候往往不能满足我们的需求,因为他只有一个文本信息,个人认为更多的就是一个文本展示,如一些用户的使用说明等,下篇为大家带来Sp的存储与读取,可通过以下链接进行跳转查看。
sp存储方式:http://blog.csdn.net/QQ_20451879/article/details/54983207
新闻热点
疑难解答