在开始之前先要搞明白的一点是,何为输入输出流,有java流基础的读者可以先跳过这一部分。
输入输出流,或是读写操作,在我看来十分容易混淆,但只要将谁在输入输出、谁在读写、被输入输出或读写的对象又是谁,这一点弄清楚就可以了:
程序输出内容,或者说写入内容到计算机的文件中,这便是输出流和写入操作;
反过来,程序读取计算机的内容,或者说计算机向程序输入内容,这就是读取操作和输入流。
将数据存储到文件
Context类中提供了一个openFileOutput方法,可以将数据存储到指定的文件,它接收两个参数:文件名和文件的操作模式。
前者不包含文件的路径,因为所有文件都默认存储到/data/data/<package name>/files/目录;后者主要有MODE_PRIVATE和MODE_APPEND两种模式,而默认是前者,他表示指定相同文件名是会覆盖源文件,后者则表示若存在则追加内容,不存在则创建新文件。
openFileOutput方法返回的是一个FileOutputStream对象,得到它我们就可以使用java流的方式,将数据写入文件中了:
fun save(inputText: String) {
try {
val output = openFileOutput("data", Context.MODE_PRIVATE)
val writer = BufferedWriter(OutputStreamWriter(output))
writer.use {
it.write(inputText)
}
} catch (e: IOException) {
e.printStackTrace()
}
}
此处通过openFileOutput方法获得一个FileOutputStream对象,将其传入OutputStreamWriter方法,即可获得一个OutputStreamWriter对象,再将其传入BufferedWriter方法,此时我们就可以通过它写入内容到文件中了。
此处我们还使用了use的kotlin内置方法,此方法会自动关闭文件流,于是不需要我们再写一个finally代码块单独关闭了。
接下来我们进入实战,新建一个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/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type something here" />
</LinearLayout>
我们创建了一个文本输入框,此时我们输入内容再点击返回键,输入的内容肯定就消失了,因为他只是瞬时数据,点击返回键时activity被销毁,而它的数据也会被回收。此时我们要做的就是存储这串瞬时数据,修改MainActivity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onDestroy() {
super.onDestroy()
//获取输入框中的文字
val inputText = editText.text.toString()
//存储到文件中
save(inputText)
}
private fun save(inputText: String) {
try {
val output = openFileOutput("data", Context.MODE_PRIVATE)
val writer = BufferedWriter(OutputStreamWriter(output))
writer.use {
it.write(inputText)
}
} catch (e: IOException) {
e.printStackTrace()
}
}
}
按下返回键,此时我们的文字就已保存在文件data中,可以通过DeviceFileExplorer工具查看/data/data/com.example.filepersistencetest/files/目录下的data文件。
不过仅做到存储还不够,我们需要在下次打开时还能让它出现在输入框中。
从文件中读取数据
类似于写入,Context类也提供了一个openFileInput方法,用于从文件中读取数据。它只接受一个参数,即需要读取的文件名,系统会自动到/data/data/com.example.filepersistencetest/files/目录下加载这个文件,并返回一个FileInputStream对象,然后我们就可以通过输入流的方式读取了:
fun load():String {
val content = StringBuilder()
try {
val input = openFileInput("data")
val reader = BufferedReader(InputStreamReader(input))
reader.use {
reader.forEachLine {
content.append(it)
}
}
} catch(e: IOException) {
e.printStackTrace()
}
return content.toString()
}
此处从文件中读取数据时,我们使用了forEachLine方法,他是kotlin的内置扩展函数,会将读取到的每行内容返回到lambda中,然后我们在lambda中完成对每行内容的拼接即可。
通过上述示例,我们修改MainActivity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//获取文件中存储的文字,若有则写入
val inputText = load()
if (inputText.isNotEmpty()) {
editText.setText(inputText)
//将光标移动到最后的位置
editText.setSelection(inputText.length)
Toast.makeText(this, "restoring succeed", Toast.LENGTH_SHORT).show()
}
}
private fun load(): String {
val content = StringBuilder()
try {
val input = openFileInput("data")
val reader = BufferedReader(InputStreamReader(input))
reader.use {
reader.forEachLine {
content.append(it)
}
}
} catch (e: IOException) {
e.printStackTrace()
}
return content.toString()
}
...
}