持久化技术简介
数据持久化指将内存中的瞬时数据保存到存储设备中,保证即使手机或电脑关机的情况下,这些数据仍然不会丢失。持久化技术则提供了一种机制可以让数据在瞬时状态和持久状态之间进行转换。
Android为我们提供了多种选项来保存永久性应用数据:
1. 共享首选项(SharedPreference): 在键值中存储私有原始数据。
2. 内部存储/文件存储: 在设备内存中存储私有数据。
3. 外部存储: 在共享的外部存储(如SD卡)中存储公共数据。
4. SQLite数据库:: 在私有数据库中存储结构化数据。
5. 网络连接: 在网络中使用我们自己的网络服务器存储数据。
内部存储
内部存储是Android中最基本的一种数据存储方式,它不对存储内容进行任何的格式化处理,所有数据都是原封不动地保存到文件中,因此它适合存储简单地文本数据或二进制数据。默认情况下,保存到内部存储地文件是应用地私有文件,其他应用和用户不能访问这些文件,当用户卸载我们的应用时,这些文件也会被移除。
将数据存储保存到文件中
Context类中提供了一个openFileOutput()方法,用于将数据存储到指定的文件中。这个方法将返回一个FileOutputStream。
openFileOutput()方法接收两个参数,第一个是文件名,文件创建时使用的就是这个名称(PS: 这里指定的文件名不可以包含路径,因为所有文件默认存储到/data/data/< packagename >/files/目录下),第二个参数是文件的操作模式,主要有MODE_PRIVATE和MODE_APPEND。
MODE_PRIVATE是默认操作模式,表示当指定同样文件名的时候,所写入的内容将覆盖原文件的内容。如果文件名不存在,则创建新的文件。
MODE_APPEND则表示当指定同样文件名的时候,所写入的内容添加到原文件中,不存在就创建新文件。
获得FileOutputStream实例后,使用write写入到文件中,最后使用close()关闭数据流。
实例:
public void save() {
//要写入的数据
String data = "Data to Save";
//创建FileOutputStream实例,并打开/创建"other data"文件
FileOutputStream output = openFileOutput("other data", Context.MODE_PRIVATE);
//调用write方法,把data转化为byte类型后写入
output.write(data.getBytes());
//最后关闭output
output.close();
应用DataBinding(Databinding使用)的完整例子:
布局文件
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="@+id/activity_mail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type Sthing"/>
</LinearLayout>
</layout>
Java文件:
public class MainActivity extends AppCompatActivity {
ActivityMainBinding mBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this,
R.layout.activity_main);
}
@Override
protected void onDestroy() {
//重写onDestroy方法
super.onDestroy();
//利用Databing简化findViewById
//获得EditText的实例
String inputText = mBinding.edit.getText().toString();
//调用自定义的save方法
//把inputText输入的数据保存到文件中
save(inputText);
}
//自定义方法
public void save(String inputText) {
//初始化FileOutputStream
FileOutputStream output = null;
//初始化BufferedWriter
BufferedWriter writer = null;
try {
//调用openFileOutput方法
output = openFileOutput("data", Context.MODE_PRIVATE);
//利用OutputStreamWriter构建BufferedWriter
writer = new BufferedWriter(new OutputStreamWriter(output));
//调用write方法写入inputText的数据
writer.write(inputText);
} catch (IOException e) {
//异常处理
e.printStackTrace();
} finally {
try {
//确保writer关闭
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
接着借助Android Device Monitor工具查看数据是否保存成功
在Android Studio的导航栏中打开
Tools > Android > Android Device Monitor
然后进入File Explorer标签页。
查找data/data/包名.项目名/files/目录中生成了一个data文件。
然后使用
把文件导出至电脑,用记事本查看。
从文件中读取数据
Context类中还提供了一个 openFileInput() 方法用于从文件中读取数据。这个方法只接收一个参数——要读取的文件名,并返回一个FileInputStream对象。然后调用read()方法读取文件字节,最后使用close()方法关闭数据流。
ps:如果在编译过程中想要保存应用中的静态文件,请在项目的res/raw/目录中保存该文件。可以使用openRawResource()方法打开该资源并传递R.raw.<filename>资源ID。此方法将返回一个InputStream,我们可以使用这个数据流读取文件(但不能写入文件)。
在刚才的完整示例中应用此方法读取数据:
public static load() {
//初始化FileInputStream
FileInputStream in = null;
//初始化BufferedReader
BufferedReader reader = null;
//创建StringBuilder对象
StringBuilder content = new StringBuilder();
try {
//调用openFileInput方法获取FileInputStream对象
in = openFileInput("data");
//利用FileInputStream对象构建BufferedReader对象
reader = new BufferedReader(new InputStreamReader(in));
//使BufferedReader一行行读取数据
//并把读取到的数据保存到StringBuilder中
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();
}