Android—數據存儲方式(一)

Android中的數據存儲方式

1、文件存儲

 思維導圖:


         文件存儲是 Android 中最基本的一種數據存儲方式,它不對存儲的內容進行任何的格式化處理,所有數據都是原封不動的保存到文件當中的。它比較適合用於存儲一些簡單的文本數據或二進制數據。如果你想使用文件存儲方式來保存一些較爲複雜的文本數據,就需要定義一套自己的格式規範,方便於之後將文件重新解析出來。

注意:在進行手機讀寫的時候需要注意一下幾點

①、判讀手機內存卡是否可用

    //獲取手機內存卡路徑
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            sdCard = Environment.getExternalStorageDirectory().getAbsolutePath();
        }

②、在AndroidManifest.xml  取得權限

  <!-- 內存卡的讀寫權限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


    ①可以使用Android  Content類中自帶的openFileOutput()方法寫入數據, openFileInput() 方法讀取數據

  openFileOutput這個方法自帶兩個參數:

           第一個參數是文件創建時使用的名稱,名稱只是名字不能帶有路徑,因爲所有文件都是默      認存儲到/data/data/<包名>/files/ 目錄下的。

           第二個參數是文件的操作模式,主要有兩種模式可以選擇, MODE_PRIVATE(覆蓋原文) 和 MODE_APPEND(追加內容)

          openFileOutput() 方法返回的是一個 FileOutputStream 對象,得到這個對象之後就可以使用 Java 流的方式將數據寫入到文件中了。

openFileInput() 方法只接收一個參數,即要讀取的文件名,然後系統會自動到 /data/data/<包名>/files/ 目錄下去加載這個文件,並返回一個 FileInputStream 對象


代碼:

 寫 

   public void write(View view){

        String first=write_baozi.getText().toString();  //寫的內容
        String fileName=write_name.getText().toString();  //文件的名字
        try { //設置文件名字,已經存儲方式
            FileOutputStream fos= openFileOutput(fileName, Context.MODE_PRIVATE);
            //向文件中寫入數據
            fos.write(first.getBytes());
            fos.close();//關閉流
            Toast.makeText(this, "保存成績!", Toast.LENGTH_SHORT).show();//提示
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


 讀

 public void read(View view){
        String fileName=write_name.getText().toString();
        try { //設置要打開的存儲文件的名字
            FileInputStream fis=openFileInput(fileName);

            byte bt[]=new byte[1024];
            int len=0;
            StringBuffer sb=new StringBuffer();
            while((len=fis.read(bt))!=-1){
                sb.append(new String(bt,0,len));
            }
            read_baozi.setText(sb);
          fis.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    ②可以使用和Java一樣讀寫文件

    Java的寫

public void write(View view){

        String first=write_baozi.getText().toString();  //寫的內容
        String fileName=write_name.getText().toString();  //文件的名字
        try {
              //sdCard手機內存卡路徑 
           FileOutputStream fos=new FileOutputStream(sdCard+"/"+fileName);
            fos.write(first.getBytes());
            fos.close();
            Toast.makeText(this, "保存成績!", Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    Java的讀

   

 public void read(View view){
        String fileName=write_name.getText().toString();
        try {
               //sdCard 內存卡路徑
            FileInputStream fis=new FileInputStream(sdCard+"/"+fileName);
            byte bt[]=new byte[1024];
            int len=0;
            StringBuffer sb=new StringBuffer();
            while((len=fis.read(bt))!=-1){
                sb.append(new String(bt,0,len));
            }
            read_baozi.setText(sb);
          fis.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

2、sharedPreference存儲數據(可實現記住密碼、自動登錄)

  思維導圖:

代碼:

package com.example.myapplication;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {

    private EditText euname;
    private EditText eupass;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        euname = (EditText) findViewById(R.id.uname);
        eupass = (EditText) findViewById(R.id.upass);

        sp = getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        editor = sp.edit();
        String uname=sp.getString("uname","");
        String upass=sp.getString("upass","");
        euname.setText(uname);
        eupass.setText(upass);
    }

    public void login(View view){
        String uname=euname.getText().toString();
        String upass=eupass.getText().toString();
        Toast.makeText(this, "跳轉頁面", Toast.LENGTH_SHORT).show();
        editor.putString("uname",uname);
        editor.putString("upass",upass);
        //細節:
        editor.commit();
    }
}

效果:

     第一次     第二次



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章