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

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

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

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

1、文件存儲

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

2、示例

在這里設(shè)置一個簡單的應(yīng)用場景:當(dāng)在文本框中輸入內(nèi)容時,當(dāng)下次再進入時顯示上次輸入的內(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;
/**
 * 文件存儲:寫入與讀取
 * @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)容不為空,則加載進來
      editText.setText(inputContent);
      //設(shè)置光標(biāo)位置,使之位于文本末尾,默認是在文本頭部
      editText.setSelection(inputContent.length());
    }
  }
  /**
   * 當(dāng)活動銷毀時,保存輸入的內(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中的方法,用于存儲數(shù)據(jù)
      //第一個參數(shù)是文件名
      //第二個參數(shù)是寫入模式,表示覆蓋寫入,如果原來有內(nèi)容,則會覆蓋
      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;
  }
}

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

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

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

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

3、結(jié)果

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

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

相關(guān)文章

  • 淺談Android LruCache的緩存策略

    淺談Android LruCache的緩存策略

    這篇文章主要介紹了淺談Android LruCache的緩存策略,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • Android中Glide獲取圖片Path、Bitmap用法詳解

    Android中Glide獲取圖片Path、Bitmap用法詳解

    這篇文章主要介紹了Android中Glide獲取圖片Path、Bitmap用法以及代碼分析,需要的朋友們參考一下吧。
    2017-12-12
  • Android入門之源碼開發(fā)基礎(chǔ)教程

    Android入門之源碼開發(fā)基礎(chǔ)教程

    這篇文章主要介紹了Android入門之源碼開發(fā)基礎(chǔ)教程,分析了環(huán)境搭建、模擬器使用及編譯文件的相關(guān)技巧與注意事項,需要的朋友可以參考下
    2016-02-02
  • Android實現(xiàn)掃描二維碼功能

    Android實現(xiàn)掃描二維碼功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)掃描二維碼功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • Android關(guān)鍵字persistent詳細分析

    Android關(guān)鍵字persistent詳細分析

    這篇文章主要介紹了Android關(guān)鍵字persistent的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-04-04
  • 動態(tài)添加LinearLayout的高度實例

    動態(tài)添加LinearLayout的高度實例

    下面小編就為大家?guī)硪黄獎討B(tài)添加LinearLayout的高度實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Android實現(xiàn)記事本功能

    Android實現(xiàn)記事本功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)記事本功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android 實現(xiàn)雙擊退出的功能

    Android 實現(xiàn)雙擊退出的功能

    本文主要介紹Android 實現(xiàn)雙擊退出,這里給大家提供代碼示例,方便大家理解查看,有需要的小伙伴可以參考下
    2016-07-07
  • Android Studio項目中導(dǎo)入開源庫的方法

    Android Studio項目中導(dǎo)入開源庫的方法

    這篇文章主要介紹了Android Studio項目中導(dǎo)入開源庫的方法,即使用第三方庫、第三廣場框架的方法,需要的朋友可以參考下
    2015-06-06
  • Android編程之截屏實現(xiàn)方法(包括scrollview與listview)

    Android編程之截屏實現(xiàn)方法(包括scrollview與listview)

    這篇文章主要介紹了Android編程之截屏實現(xiàn)方法,包括截取scrollview與listview屏幕的相關(guān)技巧,以及截屏圖片的生成與保存技巧,需要的朋友可以參考下
    2015-11-11

最新評論