這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)如何在Android開發(fā)中利用數(shù)據(jù)持久化存儲文件,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了洮北免費(fèi)建站歡迎大家使用!
數(shù)據(jù)持久化
數(shù)據(jù)持久化, 就是將內(nèi)存中的瞬時數(shù)據(jù)保存在存儲設(shè)備中, 保證即便關(guān)機(jī)之后, 數(shù)據(jù)仍然存在.
保存在內(nèi)存中的數(shù)據(jù)是瞬時數(shù)據(jù), 保存在存儲設(shè)備中的數(shù)據(jù)就是處于持久狀態(tài)的.
持久化技術(shù)則是提供了一種機(jī)制可以讓數(shù)據(jù)在瞬時狀態(tài)和持久狀態(tài)之間進(jìn)行轉(zhuǎn)換, Android系統(tǒng)中主要提供了3種方式用于簡單地實現(xiàn)數(shù)據(jù)持久化功能, 即文件存儲, SharePreference存儲, 以及數(shù)據(jù)庫存儲. 當(dāng)然你也可以將數(shù)據(jù)保存在手機(jī)的SD卡中.
文件存儲
文件存儲是android中最基本的一種數(shù)據(jù)存儲方式, 它不對存儲的內(nèi)容進(jìn)行任何的格式化處理, 所有的數(shù)據(jù)都是原封不動地保存到文件當(dāng)中, 因為他比較適合存儲一些簡單的文本數(shù)據(jù)或二進(jìn)制數(shù)據(jù). 如果你希望使用文件存儲的方式來保存一些較為復(fù)雜的的文本數(shù)據(jù), 就需要定義一套自己的格式規(guī)范, 這樣可以方便之后將數(shù)據(jù)從文件中重新取出來.
將數(shù)據(jù)存儲在文件中
Context類中提供了一個openFileOutput()方法, 可以用于將數(shù)據(jù)存儲在指定的文件中. 這個方法接收兩個參數(shù),
第一個參數(shù)是文件名, 在文件創(chuàng)建的時候使用的就是這個名稱, 注意這里指定的文件名不可以包含路徑的. 因為所有的文件都是默認(rèn)存儲到/data/data/<package name>/files/目錄下.
第二個參數(shù)是文件的操作模式, 主要有兩種模式可以選, MODE_PRIVATE和MODE_APPEND. 其中MODE_PRIVATE是默認(rèn)的操作模式, 表示當(dāng)指定同樣文件名的時候, 所寫入的內(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)容寫入到文件中了.
下面我們來一個完整的例子來理解一下,當(dāng)我們在退出程序之前, 將我們在文本框中輸入的內(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(); } } } }
那么我們運(yùn)行程序, 我們輸入的內(nèi)容就會保存在文件中. 如果您的手機(jī)已經(jīng)Root了, 可以直接在 應(yīng)用程序的包名/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對象進(jìn)行一行行的讀取, 把文件中的所有內(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()表示將光標(biāo)移動在文本框的末尾位置, 以便繼續(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對象進(jìn)行一行行的讀取, 把文件中的所有內(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ù)持久化存儲文件了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前標(biāo)題:如何在Android開發(fā)中利用數(shù)據(jù)持久化存儲文件
網(wǎng)站路徑:http://muchs.cn/article18/pdjcdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、微信公眾號、做網(wǎng)站、網(wǎng)站設(shè)計公司、小程序開發(fā)、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)