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

首頁 > 系統 > Android > 正文

Android持久化技術之文件的讀取與寫入實例詳解

2020-04-11 10:58:19
字體:
來源:轉載
供稿:網友

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

1、文件存儲

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

2、示例

在這里設置一個簡單的應用場景:當在文本框中輸入內容時,當下次再進入時顯示上次輸入的內容。

(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;/** * 文件存儲:寫入與讀取 * @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);    //首先加載上次輸入的內容    String inputContent = readFromFile();    if(!TextUtils.isEmpty(inputContent)){      //如果上次輸入的內容不為空,則加載進來      editText.setText(inputContent);      //設置光標位置,使之位于文本末尾,默認是在文本頭部      editText.setSelection(inputContent.length());    }  }  /**   * 當活動銷毀時,保存輸入的內容   */  @Override  protected void onDestroy() {    super.onDestroy();    //獲取輸入的內容    String data = editText.getText().toString();    //寫入文件    writeToFile(data);  }  /**   * 輸入的內容寫入文件   */  public void writeToFile(String data){    FileOutputStream fileOutputStream = null;    BufferedWriter bufferedWriter = null;    try {      //Context中的方法,用于存儲數據      //第一個參數是文件名      //第二個參數是寫入模式,表示覆蓋寫入,如果原來有內容,則會覆蓋      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{        //關閉流        try {          if(bufferedWriter!=null){            bufferedWriter.close();          }        } catch (IOException e) {          e.printStackTrace();        }    }  }  /**   * 從文件中讀取數據   * @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{      //關閉流      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;  }}

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

當寫入時只有文件名,當然可以添加后綴,沒有路徑,是默認存儲的。

文件的存儲時在活動銷毀時進行的。

文件內容的加載是在活動創建時進行的。

3、結果

當再次進入時,會加載上次輸入的內容,并且光標位于末尾。

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 友谊县| 于田县| 南郑县| 永兴县| 京山县| SHOW| 满城县| 江门市| 德保县| 普洱| 漾濞| 炉霍县| 阿坝| 治多县| 乌鲁木齐市| 卢龙县| 深州市| 万源市| 东平县| 白山市| 化州市| 唐山市| 龙里县| 和龙市| 工布江达县| 五华县| 咸阳市| 温泉县| 闵行区| 大理市| 平泉县| 分宜县| 天门市| 永善县| 年辖:市辖区| 库尔勒市| 隆尧县| 江津市| 和硕县| 河南省| 庐江县|