Android系统中主要提供了3种方式用于简单地实现数据持久化功能,即文件存储,SharedPreferences存储,数据库存储。下面我先介绍下文件存储。
文件存储是Android中最基本的一种数据存储方式,它比较适合用于存储一些简单的文本数据或二进制数据。
1.将数据存储到文件中
Context类中提供了一个openFileOutput()方法,可以用于将数据存储到指定的文件中。
下面我们编写一个简单的例子,学习一下如何在Android项目中使用文件存储的技术。首先创建一个FilePersistenceTest项目,修改activity_main.xml中代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type something here"
/>
</LinearLayout>
在局部中加入了一个EditText,用于输入文本内容。
修改MainActivity中的代码,如下:
public class MainActivity extends Activity {
private EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取EditText实例
edit=(EditText)findViewById(R.id.edit);
}
protected void onDestroy(){
super.onDestroy();
//获取EditText中的内容
String inputText=edit.getText().toString();
//在活动销毁前,调用save()方法把输入的内容存储到文件中
save(inputText);
}
private void save(String inputText) {
FileOutputStream out=null;
BufferedWriter writer=null;
try {
//openFileOutput()方法,将数据存储到指定的文件中
// 第一个参数,文件命名为data,没有包含路径,因为所有文件默认存储到/data/data/<packagename>/file/目录
// 第二个参数,文件的操作模式MODE_PRIVATE,表示当指定同样文件名时,所写入的内容将会覆盖原文件的内容
//通过openFileOutput()方法得到FileOutputStream对象,再借助它构建出一个OutputStreamWriter对象,
// 接着再使用OutputStreamWriter构建出一个BufferedWriter对象
// 就使用BufferedWriter对象将数据写入文件
out=openFileOutput("date", Context.MODE_PRIVATE);
writer=new BufferedWriter(new OutputStreamWriter(out));
writer.write(inputText);
} catch (IOException e) {
e.printStackTrace();
}
finally {
if(writer!=null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
运行程序,输入内容后,再按下Back键关闭程序,输入的内容已保存到文件中。
可通过Android Device Monnitor工具查看,如果导航栏中Tools—>Android找不到Android Device Monnitor,可直接在右边的点击搜索图标搜索Android Device Monnitor,点击了Android Device Monnitor—>File Explorer—>/data/data/<com.example.filepersistencetest>/files/目录,可以看到生成了一个data文件,点击可查看。也可点击右上方左按钮将文件导出到电脑上,使用记事本打开文件。
2.从文件中读取数据
类似于将数据存储到文件中,Context类中还提供了一个openFileInput()方法,用于从文件中读取数据。这个方法只接收一个参数,即要读取的文件名。修改MainActivity代码如下:
public class MainActivity extends Activity {
private EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取EditText实例
edit=(EditText)findViewById(R.id.edit);
String inputText=load();
//调用load()读取文本内容,如果读到的内容不为空,调用EditText的setText()方法,将内容填充到EditText里,
//并调用setSelection()将输入光标移动到文本末尾以便于继续输入。
//然后弹出一句还原成功的提示。
if(!TextUtils.isEmpty(inputText)){
edit.setText(inputText);
edit.setSelection(inputText.length());
Toast.makeText(this,"Restoring succeeded",Toast.LENGTH_SHORT).show();
}
}
//读取文件中存储的文本内容
private String load() {
FileInputStream in=null;
BufferedReader reader=null;
StringBuilder content=new StringBuilder();
try {
in=openFileInput("data");
reader=new BufferedReader(new InputStreamReader(in));
String line="";
while((line= reader.readLine())!=null){
content.append(line);
}
}
catch (IOException e) {
e.printStackTrace();
}finally {
//
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content.toString();
}
protected void onDestroy(){
super.onDestroy();
//获取EditText中的内容
String inputText=edit.getText().toString();
//在活动销毁前,调用save()方法把输入的内容存储到文件中
save(inputText);
}
private void save(String inputText) {
FileOutputStream out=null;
BufferedWriter writer=null;
try {
//openFileOutput()方法,将数据存储到指定的文件中
// 第一个参数,文件命名为data,没有包含路径,因为所有文件默认存储到/data/data/<packagename>/file/目录
// 第二个参数,文件的操作模式MODE_PRIVATE,表示当指定同样文件名时,所写入的内容将会覆盖原文件的内容
//通过openFileOutput()方法得到FileOutputStream对象,再借助它构建出一个OutputStreamWriter对象,
// 接着再使用OutputStreamWriter构建出一个BufferedWriter对象
// 就使用BufferedWriter对象将数据写入文件
out=openFileOutput("date", Context.MODE_PRIVATE);
writer=new BufferedWriter(new OutputStreamWriter(out));
writer.write(inputText);
} catch (IOException e) {
e.printStackTrace();
}
finally {
if(writer!=null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
重新运行程序,内容会还原到EditText。
这样,我们就已经把文件存储方面的知识学习完了,所用到的核心技术就是Context类中提供的openFileInput()和openFileOutput(),之后就是利用Java的各种流来进行读写操作。
初次写博客,欢迎小哥哥小姐姐提出宝贵的意见~