最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android持久化技術(shù)之文件的讀取與寫入實(shí)例詳解

 更新時(shí)間:2016年01月15日 15:24:52   作者:殘缺的孤獨(dú)  
這篇文章主要介紹了Android持久化技術(shù)之文件的讀取與寫入操作,結(jié)合實(shí)例形式較為詳細(xì)的分析講述了Android持久化操作的相關(guān)技巧與具體實(shí)現(xiàn)方法,需要的朋友可以參考下

本文實(shí)例分析了Android持久化技術(shù)之文件的讀取與寫入操作。分享給大家供大家參考,具體如下:

1、文件存儲(chǔ)

(1)在Android的持久化技術(shù)中,文件存儲(chǔ)是最基本的一種數(shù)據(jù)存儲(chǔ)方式。
(2)對存儲(chǔ)的內(nèi)容部做任何處理,原樣存儲(chǔ)到文件中。
(3)Context提供了文件寫入與讀取的方法,openFileOutput:寫入到文件;openFileInput:從文件中讀取。
(4)文件寫入時(shí)模式有多種:比如是覆蓋寫入還是追加寫入等。
(5)寫入的文件默認(rèn)存儲(chǔ)在/data/data/報(bào)名/files/目錄下。

2、示例

在這里設(shè)置一個(gè)簡單的應(yīng)用場景:當(dāng)在文本框中輸入內(nèi)容時(shí),當(dāng)下次再進(jìn)入時(shí)顯示上次輸入的內(nèi)容。

(1)activity_main.xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal" >
  <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Account:" />
  <EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" >
  </EditText>
</LinearLayout>

在該布局中,有一TextView和一輸入框。

(2)MainActivity.java

package com.example.testfilestore;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.widget.EditText;
/**
 * 文件存儲(chǔ):寫入與讀取
 * @author yy
 *
 */
public class MainActivity extends Activity {
  private EditText editText;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //獲取editText對象
    editText = (EditText) findViewById(R.id.editText1);
    //首先加載上次輸入的內(nèi)容
    String inputContent = readFromFile();
    if(!TextUtils.isEmpty(inputContent)){
      //如果上次輸入的內(nèi)容不為空,則加載進(jìn)來
      editText.setText(inputContent);
      //設(shè)置光標(biāo)位置,使之位于文本末尾,默認(rèn)是在文本頭部
      editText.setSelection(inputContent.length());
    }
  }
  /**
   * 當(dāng)活動(dòng)銷毀時(shí),保存輸入的內(nèi)容
   */
  @Override
  protected void onDestroy() {
    super.onDestroy();
    //獲取輸入的內(nèi)容
    String data = editText.getText().toString();
    //寫入文件
    writeToFile(data);
  }
  /**
   * 輸入的內(nèi)容寫入文件
   */
  public void writeToFile(String data){
    FileOutputStream fileOutputStream = null;
    BufferedWriter bufferedWriter = null;
    try {
      //Context中的方法,用于存儲(chǔ)數(shù)據(jù)
      //第一個(gè)參數(shù)是文件名
      //第二個(gè)參數(shù)是寫入模式,表示覆蓋寫入,如果原來有內(nèi)容,則會(huì)覆蓋
      fileOutputStream = openFileOutput("first",Context.MODE_PRIVATE);
      bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
      bufferedWriter.write(data);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }finally{
        //關(guān)閉流
        try {
          if(bufferedWriter!=null){
            bufferedWriter.close();
          }
        } catch (IOException e) {
          e.printStackTrace();
        }
    }
  }
  /**
   * 從文件中讀取數(shù)據(jù)
   * @return
   */
  public String readFromFile(){
    FileInputStream fileInputStream = null;
    BufferedReader bufferedReader = null;
    StringBuffer stringBuffer = new StringBuffer();
    try {
      fileInputStream = openFileInput("first");
      bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
      String line = "";
      while((line = bufferedReader.readLine())!=null){
        stringBuffer.append(line);
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }finally{
      //關(guān)閉流
      try {
        if (bufferedReader != null) {
          bufferedReader.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    //返回
    return stringBuffer.toString();
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
}

在該類中,提供了兩個(gè)方法:輸入內(nèi)容寫入文件以及從文件中加載上次輸入的內(nèi)容。在這兩個(gè)方法中分別調(diào)用Context提供的方法openFileOutput和openFileInput。

當(dāng)寫入時(shí)只有文件名,當(dāng)然可以添加后綴,沒有路徑,是默認(rèn)存儲(chǔ)的。

文件的存儲(chǔ)時(shí)在活動(dòng)銷毀時(shí)進(jìn)行的。

文件內(nèi)容的加載是在活動(dòng)創(chuàng)建時(shí)進(jìn)行的。

3、結(jié)果

當(dāng)再次進(jìn)入時(shí),會(huì)加載上次輸入的內(nèi)容,并且光標(biāo)位于末尾。

希望本文所述對大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Android Touch事件分發(fā)深入了解

    Android Touch事件分發(fā)深入了解

    這篇文章主要為大家詳細(xì)介紹了Android Touch事件分發(fā),內(nèi)容很詳細(xì),感興趣的朋友可以參考一下
    2016-04-04
  • Flutter runApp GestureBinding使用介紹

    Flutter runApp GestureBinding使用介紹

    這篇文章主要為大家介紹了Flutter runApp GestureBinding使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • 詳解Android如何實(shí)現(xiàn)好的彈層體驗(yàn)效果

    詳解Android如何實(shí)現(xiàn)好的彈層體驗(yàn)效果

    當(dāng)前?App?的設(shè)計(jì)趨勢越來越希望給用戶沉浸式體驗(yàn),這種設(shè)計(jì)會(huì)讓用戶盡量停留在當(dāng)前的界面,而不需要太多的跳轉(zhuǎn),這就需要引入彈層。本篇我們就來講講彈層這塊需要注意哪些用戶體驗(yàn)
    2022-11-11
  • Android 超詳細(xì)講解fitsSystemWindows屬性的使用

    Android 超詳細(xì)講解fitsSystemWindows屬性的使用

    fitsSystemWindows屬性可以讓view根據(jù)系統(tǒng)窗口來調(diào)整自己的布局;簡單點(diǎn)說就是我們在設(shè)置應(yīng)用布局時(shí)是否考慮系統(tǒng)窗口布局,這里系統(tǒng)窗口包括系統(tǒng)狀態(tài)欄、導(dǎo)航欄、輸入法等,包括一些手機(jī)系統(tǒng)帶有的底部虛擬按鍵
    2022-03-03
  • 詳解如何在Android studio中更新sdk版本和build-tools版本

    詳解如何在Android studio中更新sdk版本和build-tools版本

    這篇文章主要介紹了如何在Android studio中更新sdk版本和build-tools版本,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Android實(shí)現(xiàn)大圖滾動(dòng)顯示效果

    Android實(shí)現(xiàn)大圖滾動(dòng)顯示效果

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)大圖滾動(dòng)顯示效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • 仿ios狀態(tài)欄顏色和標(biāo)題欄顏色一致的實(shí)例代碼

    仿ios狀態(tài)欄顏色和標(biāo)題欄顏色一致的實(shí)例代碼

    下面小編就為大家分享一篇仿ios狀態(tài)欄顏色和標(biāo)題欄顏色一致的實(shí)例代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • Android手機(jī)信號(hào)強(qiáng)度檢測詳細(xì)介紹

    Android手機(jī)信號(hào)強(qiáng)度檢測詳細(xì)介紹

    這篇文章主要介紹了Android手機(jī)信號(hào)強(qiáng)度檢測的相關(guān)資料,android定義了2種信號(hào)單位:dBm和asu。具體兩種的關(guān)系本文給大家介紹非常詳細(xì),需要的朋友可以參考下
    2016-11-11
  • Android中Button實(shí)現(xiàn)點(diǎn)擊換圖案及顏色

    Android中Button實(shí)現(xiàn)點(diǎn)擊換圖案及顏色

    大家好,本篇文章主要講的是Android中Button實(shí)現(xiàn)點(diǎn)擊換圖案及顏色,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • Android自定義View app更新動(dòng)畫詳解

    Android自定義View app更新動(dòng)畫詳解

    這篇文章給大家分享了Android自定義View app更新動(dòng)畫的相關(guān)代碼以及知識(shí)點(diǎn)內(nèi)容,有興趣的朋友參考學(xué)習(xí)下。
    2018-07-07

最新評論

金平| 喀喇沁旗| 锡林郭勒盟| 北碚区| 宜阳县| 桂平市| 故城县| 莱阳市| 诸暨市| 宜宾市| 洛南县| 青龙| 吕梁市| 泰兴市| 辽源市| 德兴市| 永川市| 京山县| 从化市| 莱芜市| 东辽县| 饶阳县| 教育| 大英县| 丹寨县| 遂溪县| 平阴县| 茶陵县| 江源县| 泌阳县| 襄樊市| 东台市| 洪江市| 固阳县| 凤冈县| 聊城市| 卓资县| 邮箱| 漳平市| 仪征市| 读书|