詳解Android開發(fā)數(shù)據(jù)持久化之文件存儲(附源碼)
其實我們在社交網(wǎng)絡上面所發(fā)出的任何信息, 都希望能夠保留下來. 那么如何實現(xiàn)呢?
數(shù)據(jù)持久化
數(shù)據(jù)持久化, 就是將內(nèi)存中的瞬時數(shù)據(jù)保存在存儲設備中, 保證即便關(guān)機之后, 數(shù)據(jù)仍然存在.
保存在內(nèi)存中的數(shù)據(jù)是瞬時數(shù)據(jù), 保存在存儲設備中的數(shù)據(jù)就是處于持久狀態(tài)的.
持久化技術(shù)則是提供了一種機制可以讓數(shù)據(jù)在瞬時狀態(tài)和持久狀態(tài)之間進行轉(zhuǎn)換, Android系統(tǒng)中主要提供了3種方式用于簡單地實現(xiàn)數(shù)據(jù)持久化功能, 即文件存儲, SharePreference存儲, 以及數(shù)據(jù)庫存儲. 當然你也可以將數(shù)據(jù)保存在手機的SD卡中.
文件存儲
文件存儲是android中最基本的一種數(shù)據(jù)存儲方式, 它不對存儲的內(nèi)容進行任何的格式化處理, 所有的數(shù)據(jù)都是原封不動地保存到文件當中, 因為他比較適合存儲一些簡單的文本數(shù)據(jù)或二進制數(shù)據(jù). 如果你希望使用文件存儲的方式來保存一些較為復雜的的文本數(shù)據(jù), 就需要定義一套自己的格式規(guī)范, 這樣可以方便之后將數(shù)據(jù)從文件中重新取出來.
將數(shù)據(jù)存儲在文件中
Context類中提供了一個openFileOutput()方法, 可以用于將數(shù)據(jù)存儲在指定的文件中. 這個方法接收兩個參數(shù),
第一個參數(shù)是文件名, 在文件創(chuàng)建的時候使用的就是這個名稱, 注意這里指定的文件名不可以包含路徑的. 因為所有的文件都是默認存儲到/data/data/<package name>/files/目錄下.
第二個參數(shù)是文件的操作模式, 主要有兩種模式可以選, MODE_PRIVATE和MODE_APPEND. 其中MODE_PRIVATE是默認的操作模式, 表示當指定同樣文件名的時候, 所寫入的內(nèi)容將會覆蓋原文件中的內(nèi)容. 而MODE_APPEND則表示如果該文件已存在, 就往文件里面追加內(nèi)容, 不存在就創(chuàng)建新文件.
openFileOutput()方法返回的是一個FileOutputStream對象, 得到了這個對象之后就可以使用java流的方式將數(shù)據(jù)寫入到文件中了.
public void save(){
String data = "Data to save";
FileOutputStream out = null;
BufferedWriter writer = null;
try {
out = openFileOutput("data", Context.MODE_PRIVATE);
writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(data);
}catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(writer!= null){
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
說明: 通過openFileOutput()方法能夠得到一個FileOutputStream對象, 然后再借助它構(gòu)建出一個OutputStreamWriter對象, 接著再使用OutputStreamWriter構(gòu)建出一個BufferedWriter對象, 這樣就可以通過BufferedWriter來講文本內(nèi)容寫入到文件中了.
下面我們來一個完整的例子來理解一下,當我們在退出程序之前, 將我們在文本框中輸入的內(nèi)容儲存在文件中.
新建項目FilePersistenceDemo項目, 且修改activity_main.xml中的代碼.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
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" />
</LinearLayout>
說明: 界面只有一個EditText文本框.
MainActivity.java文件:
public class MainActivity extends AppCompatActivity {
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取editText實例
editText = (EditText)findViewById(R.id.edit);
}
// 重寫onDestroy(), 可以保證活動銷毀之前一定會調(diào)用這個方法.
@Override
protected void onDestroy() {
super.onDestroy();
String inputText = editText.getText().toString();
save(inputText);
}
public void save (String inputText){
FileOutputStream out = null;
BufferedWriter writer = null;
try {
out = openFileOutput("data", Context.MODE_PRIVATE);
writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(inputText);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(writer!= null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
那么我們運行程序, 我們輸入的內(nèi)容就會保存在文件中. 如果您的手機已經(jīng)Root了, 可以直接在 應用程序的包名/files目錄就可以發(fā)現(xiàn).
從文件中讀取數(shù)據(jù)
核心代碼:
public String load (){
FileInputStream in = null;
BufferedReader reader = null;
StringBuilder content = new StringBuilder();
try {
//獲取FileInputStream對象
in = openFileInput("data");
//借助FileInputStream對象, 構(gòu)建出一個BufferedReader對象
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
//通過BufferedReader對象進行一行行的讀取, 把文件中的所有內(nèi)容全部讀取出來
// 并存放在StringBuilder對象中
while ((line = reader.readLine())!= null){
content.append(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//最后將讀取到的內(nèi)容返回
return content.toString();
}
修改我們MainActivity中的代碼:
public class MainActivity extends AppCompatActivity {
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取editText實例
editText = (EditText)findViewById(R.id.edit);
String inputText = load();
//TextUtils.isEmpty()可以一次性判斷兩種非空判斷 傳入null或者空, 都返回true
if(!TextUtils.isEmpty((inputText))){
editText.setText(inputText);
//setSelection()表示將光標移動在文本框的末尾位置, 以便繼續(xù)輸入
editText.setSelection(inputText.length());
//彈出Toast, 給出一個提示, 表示讀取數(shù)據(jù)成功
Toast.makeText(this, "讀取數(shù)據(jù)成功!", Toast.LENGTH_SHORT).show();
}
}
// 重寫onDestroy(), 可以保證活動銷毀之前一定會調(diào)用這個方法.
@Override
protected void onDestroy() {
super.onDestroy();
String inputText = editText.getText().toString();
save(inputText);
}
public void save (String inputText){
FileOutputStream out = null;
BufferedWriter writer = null;
try {
out = openFileOutput("data", Context.MODE_PRIVATE);
writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(inputText);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(writer!= null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String load (){
FileInputStream in = null;
BufferedReader reader = null;
StringBuilder content = new StringBuilder();
try {
//獲取FileInputStream對象
in = openFileInput("data");
//借助FileInputStream對象, 構(gòu)建出一個BufferedReader對象
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
//通過BufferedReader對象進行一行行的讀取, 把文件中的所有內(nèi)容全部讀取出來
// 并存放在StringBuilder對象中
while ((line = reader.readLine())!= null){
content.append(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//最后將讀取到的內(nèi)容返回
return content.toString();
}
}
效果演示

源碼地址:FilePersistenceDemo_jb51.rar
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android8.1 源碼修改之插入SIM卡默認啟用Volte功能
這篇文章主要介紹了Android8.1 源碼修改之插入SIM卡默認啟用Volte功能,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05
Kotlin FrameLayout與ViewPager2控件實現(xiàn)滾動廣告欄方法
這篇文章主要介紹了Kotlin FrameLayout與ViewPager2控件實現(xiàn)滾動廣告欄,F(xiàn)rameLayout與ViewPager2是Android開發(fā)中非常常見的布局組件,并且它不單單是一個幀布局組件,可以用它實現(xiàn)多種功能,感興趣的朋友一起來看看吧2022-12-12
Android開發(fā)之獲取短信驗證碼后按鈕背景變化并且出現(xiàn)倒計時
在開發(fā)是經(jīng)常會遇到獲取短信驗證碼,然后獲取驗證碼后需要等待n秒倒計時,這時是不能再次發(fā)送短信請求的,這是需要一個倒計時程序,本文給大家分享了實現(xiàn)此功能的代碼,需要的朋友參考下2016-01-01

