Android資料儲存筆記

2021-08-03 21:16:00 字數 1420 閱讀 1852

1.sharedpreferences  用來儲存少量的資料,且資料格式簡單(字串,boolean等),主要儲存應用程式的配置資訊

sharedpreferences儲存的資料主要是 key-value對。

sharedpreferences 本身是乙個介面,無法直接建立例項,可通過以下方法獲取例項:

1. context類中的 getsharepreferences(string name,int mode)

2. activity類中的  getpreferences(int mode)

3. preferencemanager類中的getdefaultsharepreferences(context context)

mode型別目前只有mode_private(==0) 可選, 其他均已廢棄。  

name代表sharepreferences檔名

訪問**如下:   與sharedpreferences.editor配合使用

sharedpreferences.editor editor = getsharedpreferences("data",mode_private).edit();

editor.putstring("name","tom");

editor.putint("age",28);

string name = getsharedpreferences("data",mode_private).getstring("name",null);

int age = getsharedpreferences("data",mode_private).getint("age",0);

2.檔案(file 儲存)  

適合用於儲存一些簡單的文字資料或二進位制資料

寫入資料;

string data = "this data";

fileoutputstream out = null;

bufferedwriter writer =null;

try catch (exception e)finally

}catch (exception e)

}

讀取資料:

fileinputstream in = null;

bufferedreader reader = null;

stringbuilder content = new stringbuilder();

try

} catch (exception e) finally catch (exception e)

}}

3.資料庫儲存  

android系統內建了乙個sqlite輕量級資料庫,也有許多開源資料庫框架可使用,如 greendao等

Android 學習筆記14 資料儲存

android的資料儲存有4中方式 sharedpreferences sqlite content provider和file sharepreferences 提供輕量型資料儲存,一般使用者儲存配置資訊 本質上是xml檔案上的鍵值對,通常用來儲存一些簡單的配置資訊。其儲存位置在 data dat...

Android資料儲存

android中一共提供了4種資料儲存方式 shared preferences 用來儲存 key value paires 格式的資料,它是乙個輕量級的鍵值儲存機制,只可以儲存基本資料型別。files 他通過fileinputstream和fileoutputstream對檔案進行操作。但是在an...

Android資料儲存

1.五種儲存方式 android作業系統提供了一種公共檔案系統,即任何應用軟體都可以使用它來儲存和讀取檔案,該檔案被其他的應用軟體讀取。android採用了一種不同的系統,在android中,所有的應用軟體資料 為應用軟體私有,然而,android也提供了一種標準方式 用軟體將私有資料開放給其他應用...