
前言
學習Android相關知識,資料儲存是其中的重點之一,如果不瞭解資料,那麼讓你跟一款沒有資料的應用玩,你能玩多久呢?答案是這和沒有手機幾乎是差不多的。我們聊QQ,聊微信,看新聞,刷朋友圈等都是看裡面的資料,所以在Android中資料對我們是多麼重要。
資料,如今是資料大時代,誰擁有資料,誰就能掌握未來,這一點很可怕的,現在你用的手機APP中存在著你的大量資料資訊,大資料的積累可以掌握出你的作息時間,生活規律等。
對資料的儲存有著良好的技術支援,是一個好的開發平臺的體現,如果不能長時間保持資料,那麼必然會被時代發展所淘汰。那麼有長期保持資料的概念,就有瞬時資料這一概念的出現,什麼是瞬時資料呢?
見名知意,瞬時代表一瞬間,指當儲存的資料因程式關閉或其他原因等導致資料丟失,如果你想發個自拍發個朋友圈,可是一重新整理就沒了,是不是很惱怒呢?氣不氣,氣不氣,是不是想砸手機?
本篇主要介紹Android中實現資料儲存的三種方式:
- 檔案儲存
- SharedPreference儲存
- SQLite資料庫儲存
下面給出三種方式的資料檔案預設儲存的位置:

在DDMS裡的File Explore下開啟data資料夾,切換到DDMS檢視,點選FileExplorer切換卡,進入到/data/data//files/目錄下,可以看到檔案了。
DDMS檔案的匯出

好了,接下來分析資料儲存方式了。如上圖為檔案儲存位置,接下講第一個檔案儲存。然後再接著講SharedPreferences儲存和SQLite資料庫儲存。
檔案儲存
檔案儲存是Android中最基本的一種儲存方式,和Java中實現I/O的方式,由Context類提供openFileInput()和openFileOutput()方法開啟。檔案儲存主要分兩種儲存,一種是內部儲存,一種是外部儲存。
記憶體儲存:使用了FileInputStream類中的openFileInput()方法,用於讀取資料;使用了FileOutputStream類中的openFileOutput()方法,用於寫入資料。
外部儲存:使用Enviroment類中的getExternalStorageDirectory()方法對外部儲存上的檔案進行讀寫。
簡化:
FileOutputStream -> openFileOutput() FileInputStream -> openFileInput() Environment -> getExternalStorageDirectory()
一,內部儲存的寫入檔案步驟
第一, 獲取檔案輸出流物件 FileOutputStream
第二, 使用FileOutputStream類的openFileOutput(String name, int mode)方法
第三, 呼叫FileOutputStream物件的write()方法寫入檔案
第四, 呼叫flush()方法,因為write()方法是寫入緩衝區的,呼叫flush()方法將緩衝中的資料寫入到檔案,清空快取
第五, close()方法用於關閉FileOutputStream
二,內部儲存的讀取檔案步驟
第一, 獲取檔案輸入流物件 FileInputStream
第二, 使用FileInputStream類的openFileInput()方法來實現
第三, 呼叫FileInputStream物件的read()方法
第四, 呼叫close()關閉檔案輸入流物件
如上分別描述了內部儲存的寫入檔案步驟和讀取檔案步驟,那麼接下來程式碼例子展示一下這些步驟。
寫入資料

在openFileOutput()方法中,接收兩個引數,一個是檔名,另一個是檔案的操作模式,主要有兩種為 MODE_PRIVATE和MODE_APPEND。最後openFileOutput()方法會丟擲FileNotFoundException。
主要程式碼步驟:

讀取資料

讀取檔案內容的使用程式碼:


綜上,在Android中讀取與寫入檔案的方法,和Java中實現I/O的程式是一樣的,提供了openFileInput()和openFileOutput()方法來讀取裝置上的檔案。通過Context.openFileOutput()獲取輸出流,引數分別為檔名和儲存模式;通過Context.openFileInput()獲取輸入流,引數為檔名。
介紹了內部儲存,現在介紹外部儲存。
外部儲存的檔案時全域性都是可以讀的,在程式中,使用Environment類的getExternalStorageDirectory方法,來讀取外部檔案。

最後外部儲存檔案,要加入讀取與寫入的許可權,新增
READ_EXTERNAL_STORAGE WRITE_EXTERNAL_STORAGE
這樣檔案儲存就講得差不多了,多練習試試。對於SharePreferences儲存和SQLite儲存,現在進入講解。
下面對SharedPreferences儲存介紹:
SharedPreferences是一種資料儲存方式,用來儲存一些簡單的資訊,位於android.content包中,以鍵值對的模式進行儲存,鍵值對(key-value)。對於儲存的基本資訊,如int,string,boolean,float和long。
- 使用getSharedPreferences()方法獲取
- 使用getPreferences()方法獲取

實現SharedPreferences儲存步驟:
第一, 呼叫SharedPreferences類的edit()方法,獲取SharedPreferences.Editor物件
第二, 呼叫一個SharedPreferences.Editor物件的putXXX()的方法獲取資料
第三, 使用commit()方法提交資料



在這裡介紹完檔案儲存和SharedPreferences,接下來講SQLite資料庫儲存。但是除了這三個,還有幾種儲存方式。如圖:

接下來講講Android中SQLite資料庫的增刪改查的操作。

SQLite資料庫儲存
SQLite是一款輕量級的關係型資料庫,它的運算很快,佔用空間小,SQLite不僅有SQL語法支援,獨立,還有資料庫的ACID事務,SQLite資料庫為一種嵌入式資料庫。
SQLiteOpenHelper類,它是SQLiteDatabase的幫助類,用於管理資料庫的建立和升級,SQLiteOpenHelper類為抽象類,有兩個要重寫的方法:onCreate()和onUpgrade(),用於去實現建立和升級資料庫。
在SQLiteOpenHelper類中還需要一個構造方法,這個方法接收四個引數,分別為Context context,String name, CursorFactory factory,int version這四個。第一個是引數context,第二個是資料庫名,第三個是自定義Cursor,一般為null,第四個為當前的資料庫版本號。
在SQLiteOpenHelper類中有兩個重要的例項方法,為getReadableDatabase()和getWritableDatabase()這兩個方法。呼叫這其中的方法就能夠建立資料庫了。如果存在資料庫就開啟,沒有就建立。
SQLiteOpenHelper類是一個SQLite幫助類,實現SQLite的資料操作,建立一個類去繼承SQLiteOpenHelper,接下來程式碼示例一下。



接下來增加資料
如何增加資料,用put()方法即可,在SQLiteDatabase中提供insert()方法,接下來也講解一下。

刪除資料
用到的方法是delete(),第一個引數為表名,第二三位用於約束。

更新資料
用到的方法是update()方法,引數分別為表名,ContentValues物件,約束,約束,好了,接下來示例一下。

查詢資料
用到的方法為query(),這個方法至少需要七個引數(table, columns, selection, selectionArgs, groupBy, having, orderBy),分別為表名,要查詢出的名,查詢條件語句,對應於selection語句中佔位符的值,要分組的列名,分組後過濾條件語句,排序方式。


升級資料庫
用到onUpdate()方法,分別刪除表,再次在此方法裡建立即可。

程式碼:
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
private static Integer Version = 1;
//建構函式
public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,
int version) {
//必須呼叫父類當中的建構函式
super(context, name, factory, version);
}
//context:上下文物件
//name:資料庫名稱
//param:factory
//version:當前資料庫的版本
public MySQLiteOpenHelper(Context context,String name,int version){
this(context,name,null,version);
}
public MySQLiteOpenHelper(Context context,String name){
this(context, name, Version);
}
//建立的時候被呼叫
@Override
public void onCreate(SQLiteDatabase db) {
//建立了資料庫並建立一個的表
String sql = "create table sut(id int primary key,name varchar(200))";
db.execSQL(sql);
}
//資料庫升級時呼叫
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
System.out.println("更新版本為:"+newVersion);
}
}
複製程式碼
結語
- Android儲存技術知識點就介紹到這了,一起努力吧~
- 小禮物走一走 or 點贊
