欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年03月01日 17:24:54   作者:junzaivip  
本篇文章主要介紹了詳解Android開發(fā)數(shù)據(jù)持久化之文件存儲(chǔ)(附源碼),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

其實(shí)我們?cè)谏缃痪W(wǎng)絡(luò)上面所發(fā)出的任何信息, 都希望能夠保留下來(lái). 那么如何實(shí)現(xiàn)呢?

數(shù)據(jù)持久化

數(shù)據(jù)持久化, 就是將內(nèi)存中的瞬時(shí)數(shù)據(jù)保存在存儲(chǔ)設(shè)備中, 保證即便關(guān)機(jī)之后, 數(shù)據(jù)仍然存在.

保存在內(nèi)存中的數(shù)據(jù)是瞬時(shí)數(shù)據(jù), 保存在存儲(chǔ)設(shè)備中的數(shù)據(jù)就是處于持久狀態(tài)的.

持久化技術(shù)則是提供了一種機(jī)制可以讓數(shù)據(jù)在瞬時(shí)狀態(tài)和持久狀態(tài)之間進(jìn)行轉(zhuǎn)換, Android系統(tǒng)中主要提供了3種方式用于簡(jiǎn)單地實(shí)現(xiàn)數(shù)據(jù)持久化功能, 即文件存儲(chǔ), SharePreference存儲(chǔ), 以及數(shù)據(jù)庫(kù)存儲(chǔ). 當(dāng)然你也可以將數(shù)據(jù)保存在手機(jī)的SD卡中.

文件存儲(chǔ)

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

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

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

第一個(gè)參數(shù)是文件名, 在文件創(chuàng)建的時(shí)候使用的就是這個(gè)名稱, 注意這里指定的文件名不可以包含路徑的. 因?yàn)樗械奈募际悄J(rèn)存儲(chǔ)到/data/data/<package name>/files/目錄下.

第二個(gè)參數(shù)是文件的操作模式, 主要有兩種模式可以選, MODE_PRIVATE和MODE_APPEND. 其中MODE_PRIVATE是默認(rèn)的操作模式, 表示當(dāng)指定同樣文件名的時(shí)候, 所寫入的內(nèi)容將會(huì)覆蓋原文件中的內(nèi)容. 而MODE_APPEND則表示如果該文件已存在, 就往文件里面追加內(nèi)容, 不存在就創(chuàng)建新文件.

openFileOutput()方法返回的是一個(gè)FileOutputStream對(duì)象, 得到了這個(gè)對(duì)象之后就可以使用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();
      }
    }
  }

說(shuō)明: 通過(guò)openFileOutput()方法能夠得到一個(gè)FileOutputStream對(duì)象, 然后再借助它構(gòu)建出一個(gè)OutputStreamWriter對(duì)象, 接著再使用OutputStreamWriter構(gòu)建出一個(gè)BufferedWriter對(duì)象, 這樣就可以通過(guò)BufferedWriter來(lái)講文本內(nèi)容寫入到文件中了.

下面我們來(lái)一個(gè)完整的例子來(lái)理解一下,當(dāng)我們?cè)谕顺龀绦蛑? 將我們?cè)谖谋究蛑休斎氲膬?nèi)容儲(chǔ)存在文件中.

新建項(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>

說(shuō)明: 界面只有一個(gè)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(), 可以保證活動(dòng)銷毀之前一定會(huì)調(diào)用這個(gè)方法.
  @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)容就會(huì)保存在文件中. 如果您的手機(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對(duì)象
      in = openFileInput("data");
      //借助FileInputStream對(duì)象, 構(gòu)建出一個(gè)BufferedReader對(duì)象
      reader = new BufferedReader(new InputStreamReader(in));
      String line = "";
      //通過(guò)BufferedReader對(duì)象進(jìn)行一行行的讀取, 把文件中的所有內(nèi)容全部讀取出來(lái)
      // 并存放在StringBuilder對(duì)象中
      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)移動(dòng)在文本框的末尾位置, 以便繼續(xù)輸入
      editText.setSelection(inputText.length());
      //彈出Toast, 給出一個(gè)提示, 表示讀取數(shù)據(jù)成功
      Toast.makeText(this, "讀取數(shù)據(jù)成功!", Toast.LENGTH_SHORT).show();
    }
  }

  // 重寫onDestroy(), 可以保證活動(dòng)銷毀之前一定會(huì)調(diào)用這個(gè)方法.
  @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對(duì)象
      in = openFileInput("data");
      //借助FileInputStream對(duì)象, 構(gòu)建出一個(gè)BufferedReader對(duì)象
      reader = new BufferedReader(new InputStreamReader(in));
      String line = "";
      //通過(guò)BufferedReader對(duì)象進(jìn)行一行行的讀取, 把文件中的所有內(nèi)容全部讀取出來(lái)
      // 并存放在StringBuilder對(duì)象中
      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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android8.1 源碼修改之插入SIM卡默認(rèn)啟用Volte功能

    Android8.1 源碼修改之插入SIM卡默認(rèn)啟用Volte功能

    這篇文章主要介紹了Android8.1 源碼修改之插入SIM卡默認(rèn)啟用Volte功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-05-05
  • Kotlin Jetpack組件ViewModel使用詳解

    Kotlin Jetpack組件ViewModel使用詳解

    作為Jetpack組件之一的ViewModel,也是框架MVVM中的一部分,其功能主要用于屏幕反轉(zhuǎn)后的數(shù)據(jù)保存;因?yàn)锳ctivity翻轉(zhuǎn)屏幕后或?qū)崿F(xiàn)onCreat()方法,也就是說(shuō)會(huì)重新創(chuàng)建頁(yè)面,之前頁(yè)面的臨時(shí)數(shù)據(jù)都會(huì)清除
    2022-12-12
  • Android編程實(shí)現(xiàn)圖片平鋪的方法分析

    Android編程實(shí)現(xiàn)圖片平鋪的方法分析

    這篇文章主要介紹了Android編程實(shí)現(xiàn)圖片平鋪的方法,結(jié)合具體實(shí)例形式總結(jié)分析了Android實(shí)現(xiàn)圖片平鋪效果的三種常用操作技巧,需要的朋友可以參考下
    2017-06-06
  • 詳解Android運(yùn)行時(shí)權(quán)限及APP適配方法

    詳解Android運(yùn)行時(shí)權(quán)限及APP適配方法

    本篇文章給大家詳細(xì)分析了Android運(yùn)行時(shí)權(quán)限及APP適配方法,并把重要知識(shí)點(diǎn)做了說(shuō)明,有需要的朋友參考下。
    2018-03-03
  • Android信息界面編輯及組合控件的封裝

    Android信息界面編輯及組合控件的封裝

    這篇文章主要為大家詳細(xì)介紹了Android信息界面的編輯,及組合控件的封裝,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Kotlin FrameLayout與ViewPager2控件實(shí)現(xiàn)滾動(dòng)廣告欄方法

    Kotlin FrameLayout與ViewPager2控件實(shí)現(xiàn)滾動(dòng)廣告欄方法

    這篇文章主要介紹了Kotlin FrameLayout與ViewPager2控件實(shí)現(xiàn)滾動(dòng)廣告欄,F(xiàn)rameLayout與ViewPager2是Android開發(fā)中非常常見的布局組件,并且它不單單是一個(gè)幀布局組件,可以用它實(shí)現(xiàn)多種功能,感興趣的朋友一起來(lái)看看吧
    2022-12-12
  • Android自定義流式布局的實(shí)現(xiàn)示例

    Android自定義流式布局的實(shí)現(xiàn)示例

    這篇文章主要介紹了Android自定義流式布局的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 解決Android Studio電腦不支持HAXM的問(wèn)題

    解決Android Studio電腦不支持HAXM的問(wèn)題

    這篇文章主要介紹了Android Studio電腦不支持HAXM的問(wèn)題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2020-03-03
  • Android簡(jiǎn)單實(shí)現(xiàn)菜單拖拽排序的功能

    Android簡(jiǎn)單實(shí)現(xiàn)菜單拖拽排序的功能

    這篇文章主要介紹了Android簡(jiǎn)單實(shí)現(xiàn)菜單拖拽排序的功能,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)價(jià)值,需要的朋友可以參考一下
    2022-07-07
  • Android開發(fā)之獲取短信驗(yàn)證碼后按鈕背景變化并且出現(xiàn)倒計(jì)時(shí)

    Android開發(fā)之獲取短信驗(yàn)證碼后按鈕背景變化并且出現(xiàn)倒計(jì)時(shí)

    在開發(fā)是經(jīng)常會(huì)遇到獲取短信驗(yàn)證碼,然后獲取驗(yàn)證碼后需要等待n秒倒計(jì)時(shí),這時(shí)是不能再次發(fā)送短信請(qǐng)求的,這是需要一個(gè)倒計(jì)時(shí)程序,本文給大家分享了實(shí)現(xiàn)此功能的代碼,需要的朋友參考下
    2016-01-01

最新評(píng)論