Android基礎教程數(shù)據(jù)存儲之文件存儲

Android基礎教程數(shù)據(jù)存儲之文件存儲

創(chuàng)新互聯(lián)是工信部頒發(fā)資質IDC服務器商,為用戶提供優(yōu)質的服務器主機托管服務

將數(shù)據(jù)存儲到文件中并讀取數(shù)據(jù)

1、新建FilePersistenceTest項目,并修改activity_main.xml中的代碼,如下:(只加入了EditText,用于輸入文本內(nèi)容,不管輸入什么按下back鍵就丟失,我們要做的是數(shù)據(jù)被回收之前,將它存儲在文件中)

<?xml version="1.0" encoding="utf-8"?>
<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">

  <EditText
    android:id="@+id/edit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Type something here"/>
</LinearLayout>

2、修改MainActivity中的代碼,如下:(save()方法將一段文本內(nèi)容保存到文件中,load()方法從文件中讀取數(shù)據(jù),套用)

public class MainActivity extends AppCompatActivity {
  private EditText edit;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edit=(EditText) findViewById(R.id.edit);
    String inputText=load();
    if(!TextUtils.isEmpty(inputText)){                 //對字符串進行非空判斷
      edit.setText(inputText);
      edit.setSelection(inputText.length());
      Toast.makeText(this,"Restoring succeeded",Toast.LENGTH_SHORT).show();
    }

  }
  @Override
  protected void onDestroy(){                      //重寫onDestroy()保證在活動銷毀之前一定調用這個方法
    super.onDestroy();
    String inputText=edit.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{
      in=openFileInput("data");
      reader=new BufferedReader(new InputStreamReader(in));
      String line="";
      while((line=reader.readLine())!=null){
        content.append(line);
      }
    }catch(IOException e){
      e.printStackTrace();
    }finally {
      if(reader!=null){
        try{
          reader.close();
        }catch (IOException e){
          e.printStackTrace();
        }
      }
    }
    return content.toString();
  }
}

運行程序,效果如下:(輸入content后按back鍵返回,重新打開)

Android基礎教程數(shù)據(jù)存儲之文件存儲

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

本文題目:Android基礎教程數(shù)據(jù)存儲之文件存儲
轉載注明:http://muchs.cn/article46/gpjseg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、App設計、品牌網(wǎng)站制作關鍵詞優(yōu)化、標簽優(yōu)化、定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設