国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

詳解Android開發(fā)數(shù)據(jù)持久化之文件存儲(附源碼)

2019-10-23 19:44:06
字體:
供稿:網(wǎng)友

其實(shí)我們在社交網(wǎng)絡(luò)上面所發(fā)出的任何信息, 都希望能夠保留下來. 那么如何實(shí)現(xiàn)呢?

數(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種方式用于簡單地實(shí)現(xiàn)數(shù)據(jù)持久化功能, 即文件存儲, SharePreference存儲, 以及數(shù)據(jù)庫存儲. 當(dāng)然你也可以將數(shù)據(jù)保存在手機(jī)的SD卡中.

文件存儲

文件存儲是android中最基本的一種數(shù)據(jù)存儲方式, 它不對存儲的內(nèi)容進(jìn)行任何的格式化處理, 所有的數(shù)據(jù)都是原封不動地保存到文件當(dāng)中, 因?yàn)樗容^適合存儲一些簡單的文本數(shù)據(jù)或二進(jìn)制數(shù)據(jù). 如果你希望使用文件存儲的方式來保存一些較為復(fù)雜的的文本數(shù)據(jù), 就需要定義一套自己的格式規(guī)范, 這樣可以方便之后將數(shù)據(jù)從文件中重新取出來.

將數(shù)據(jù)存儲在文件中

Context類中提供了一個openFileOutput()方法, 可以用于將數(shù)據(jù)存儲在指定的文件中. 這個方法接收兩個參數(shù),

第一個參數(shù)是文件名, 在文件創(chuàng)建的時候使用的就是這個名稱, 注意這里指定的文件名不可以包含路徑的. 因?yàn)樗械奈募际悄J(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)容儲存在文件中.

新建項(xiàng)目FilePersistenceDemo項(xiàng)目, 且修改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實(shí)例    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實(shí)例    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持久化存儲,android,持久化數(shù)據(jù),文件存儲

源碼地址:FilePersistenceDemo.rar

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 五家渠市| 随州市| 湖口县| 永宁县| 卢湾区| 高平市| 商丘市| 汽车| 银川市| 崇文区| 通榆县| 富源县| 彭泽县| 丹阳市| 龙游县| 钟山县| 广德县| 土默特右旗| 宝丰县| 利辛县| 越西县| 河南省| 锦屏县| 荣昌县| 萍乡市| 永川市| 于田县| 玉田县| 巨鹿县| 巧家县| 库车县| 静宁县| 石棉县| 城口县| 汽车| 德州市| 辛集市| 北票市| 龙门县| 祥云县| 龙口市|