您好,登錄后才能下訂單哦!
這期內(nèi)容當中小編將會給大家?guī)碛嘘P如何在Android開發(fā)中利用數(shù)據(jù)持久化存儲文件,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
數(shù)據(jù)持久化
數(shù)據(jù)持久化, 就是將內(nèi)存中的瞬時數(shù)據(jù)保存在存儲設備中, 保證即便關機之后, 數(shù)據(jù)仍然存在.
保存在內(nèi)存中的數(shù)據(jù)是瞬時數(shù)據(jù), 保存在存儲設備中的數(shù)據(jù)就是處于持久狀態(tài)的.
持久化技術則是提供了一種機制可以讓數(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對象, 然后再借助它構建出一個OutputStreamWriter對象, 接著再使用OutputStreamWriter構建出一個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對象, 構建出一個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對象, 構建出一個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(); } }
上述就是小編為大家分享的如何在Android開發(fā)中利用數(shù)據(jù)持久化存儲文件了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。