Android:日常學習筆記(9)———探究持久化技術

來源:互聯網
上載者:User

標籤:資料   bool   try   sha   引入   android   說明   傳遞   日常   

Android:日常學習筆記(9)———探究持久化技術引入持久化技術什麼是持久化技術

  持久化技術就是指將那些記憶體中的瞬時資料儲存到存放裝置中,保證即使在手機或電腦關機的情況下,這些資料仍然不會丟失。

Android系統提供的三種持久化技術:

  檔案儲存體、SharedPreference(使用共用喜好設定)儲存以及資料庫儲存。

檔案儲存體

說明:

  您可以直接在裝置的內部儲存中儲存檔案。預設情況下,儲存到內部儲存的檔案是應用的私人檔案,其他應用(和使用者)不能訪問這些檔案。 當使用者卸載您的應用時,這些檔案也會被移除。

要建立私人檔案並寫入到內部儲存:

  1. 使用檔案名稱和操作模式調用 openFileOutput()。 這將返回一個 FileOutputStream
  2. 使用 write() 寫入到檔案。
  3. 使用 close() 關閉串流。

要從內部儲存讀取檔案:

  1. 調用 openFileInput() 並向其傳遞要讀取的檔案名稱。 這將返回一個 FileInputStream
  2. 使用 read() 讀取檔案位元組。
  3. 然後使用 close() 關閉串流。

通用代碼

 public void save1(String text)    {        FileOutputStream out = null;        BufferedWriter writer = null;        try {            out = openFileOutput("data",MODE_PRIVATE);            writer = new BufferedWriter(new OutputStreamWriter(out));            writer.write(text);        } catch (IOException e) {            e.printStackTrace();        }finally {            if(writer!=null)                try {                    writer.close();                } catch (IOException e) {                    e.printStackTrace();                }        }    }    public void read1()    {        FileInputStream in =null;        BufferedReader reader = null;        StringBuilder content = new StringBuilder();        try {            in = openFileInput("data");            reader = new BufferedReader(new InputStreamReader(in));            String line =null;            while ((line=reader.readLine())!=null)                content.append(line);            Toast.makeText(StoreActivity.this,content,Toast.LENGTH_SHORT).show();        } catch (IOException e) {            e.printStackTrace();        }finally {            try {                reader.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }
SharedPreference儲存

說明:

  不同於檔案的儲存方式,ShardPreference是使用索引值對的方式來儲存資料的。您可以使用 SharedPreferences 來儲存任何未經處理資料:布爾值、浮點值、整型值、長整型和字串。 此資料將跨多個使用者會話持續保留(即使您的應用已終止亦如此)。

擷取SharedPreferences對象:

  • getSharedPreferences() - 如果您需要多個按名稱(使用第一個參數指定)識別的喜好設定檔案,請使用此方法。
  • getPreferences() - 如果您只需要一個用於 Activity 的喜好設定檔案,請使用此方法。 由於這將是用於 Activity 的唯一喜好設定檔案,因此無需提供名稱。也就是說會把當前活動的類名作為檔案名稱。

寫入值:

  1. 調用 edit() 以擷取 SharedPreferences.Editor
  2. 使用 putBoolean() 和 putString() 等方法添加值。
  3. 使用 commit() 提交新值

讀入值:

  要讀取值,請使用 getBoolean() 和 getString() 等 SharedPreferences 方法。

通用代碼

    public void save2()    {        SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();        editor.putString("name","Tom");        editor.putInt("age",20);        editor.apply();    }    public void read2()    {        SharedPreferences sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);        Log.d("StoreActivity",sharedPreferences.getString("name",""));        Log.d("StoreActivity",""+sharedPreferences.getInt("age",100));            }
SQLite資料庫儲存

 

 

Android:日常學習筆記(9)———探究持久化技術

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.