| ~本特利~ |
學習筆記
- 持久化技術簡介
- 第一種方式:檔案存盤
- 將資料存盤到檔案中
- 從檔案中讀取資料
- 案例-檔案存盤技術
- MainActivity.kt
- activity_main.xml
持久化技術簡介
資料持久化就是指將那些記憶體中的瞬時資料保存到存盤設備中,保證即使在手機或計算機關機的情況下,這些資料仍然不會丟失,
保存在記憶體中的資料是處于瞬時狀態的,而保存在存盤設備中的資料是處于持久狀態的,持久化技術提供了一種機制,可以讓資料在瞬時狀態和持久狀態之間進行轉換,
Android系統中主要提供了3種方式用于簡單地實作資料持久化功能:
檔案存盤、SharedPreferences存盤、資料庫存盤,
第一種方式:檔案存盤
檔案存盤是Android中最基本的資料存盤方式,它不對存盤的內容進行任何格式化處理,所有資料都是原封不動地保存到檔案當中的,因而它比較適合存盤一些簡單的文本資料或二進制資料,
如果你想使用檔案存盤的方式來保存一些較為復雜的結構化資料,就需要定義一套自己的格式規范,方便之后將資料從檔案中重新決議出來,
將資料存盤到檔案中
Context類中提供了一個openFileOutput()方法,可以用于將資料存盤到指定的檔案中,
所有的檔案會默認存盤到/data/data//files/目錄下,示例寫法如下:
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()
}
}
Context 類中提供了一個openFileOutput( )方法,讓我們可以將資料存盤在指定位置,該檔案接收兩個引數,第一個是檔案名,第二個是檔案的操作模式,主要有MODE_PRIVATE(默認的,有覆寫內容的作用)和MODE_APPEND(不覆寫但有追加和創建作用)兩種,
代碼分析:
- 通過一個openFileOutput( )方法得到FileOutputStream物件-output
- 借助output構建出OutputStreamWriter物件
- 使用OutputStreamWriter構建BufferedWriter物件-writer
- 這樣就可以通過BufferedWriter把內容寫入檔案,最后使用kotlin提供的內置擴展函式use自動將外層的流關閉,
從檔案中讀取資料
Context類中還提供了一個openFileInput()方法,用于從檔案中讀取資料,
它會自動到/data/data//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()
}
代碼分析:
- 通過一個openFileInput( )方法得到FileOutputStream物件-intput
- 借助intput構建出InputStreamReader物件
- 使用InputStreamReader構建BufferedReader物件-reader
- 這樣就可以通過BufferedReader把內容讀取檔案
- 再拼接到StringBuilder物件中,最后把讀取的內容回傳
6.函式use可自動將外層的流關閉,并在其中使用forEachLine函式,他會將讀取的內容回呼到Lambda運算式中
案例-檔案存盤技術
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//1.在onCreate()方法中呼叫load()方法讀取檔案中存盤的內容
val inputText = load()
//如果不為空,就呼叫EditText的setText()方法將內容填充到 EditText中
if (inputText.isNotEmpty()) {
editText.setText(inputText)
//再呼叫setSelection()方法將輸入游標移動到文末以便繼續輸入
editText.setSelection(inputText.length)
//然后彈出還原成功的Toast提示
Toast.makeText(this, "還原成功", Toast.LENGTH_SHORT).show()
}
}
//重寫onDestroy()方法,保證Activity在銷毀之前一定會用到的方法
override fun onDestroy() {
super.onDestroy()
//獲取EditText中輸入的內容
val inputText = editText.text.toString()
//呼叫save()方法將內容存盤到檔案中
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()
}
}
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()
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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>
總結: 核心技術就是openFileInput()和 openFileOutput()方法,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/210058.html
標籤:java
下一篇:碎片實踐-新聞應用
