
Android中實現(xiàn)數(shù)據(jù)存儲的三種方式:
1、文件存儲
2、SharedPreference存儲
3、SQLite數(shù)據(jù)庫存儲
下面給出三種方式的數(shù)據(jù)文件默認存儲的位置:

下面給出存儲的位置以及導出的方式

文件存儲
文件存儲是Android中最基本的一種存儲方式,和Java中實現(xiàn)I/O的方式,由Context類提供openFileInput()和openFileOutput()方法打開。文件存儲主要分兩種存儲,一種是內(nèi)部存儲,一種是外部存儲。
- 內(nèi)部存儲
使用了FileInputStream類中的openFileInput()方法,用于讀取數(shù)據(jù);使用了FileOutputStream類中的openFileOutput()方法,用于寫入數(shù)據(jù)。
- 外部存儲
使用Enviroment類中的getExternalStorageDirectory()方法對外部存儲上的文件進行讀寫。
注意:寫入時需要添加寫入權限
內(nèi)部存儲
- 存儲數(shù)據(jù)
1、通過openFileOutput(String name, int mode)來返回一個
FileOutputStream對象
2、通過調(diào)用fileOutputStream.write(content.getBytes());來進行寫入
write(byte b[])可以看出這里存儲的不是String,而是通過字節(jié)的形式進行存儲的
3、調(diào)用fileOutputStream.flush();因為write()方法是寫入緩沖區(qū)的,調(diào)用flush()方法將緩沖中的數(shù)據(jù)寫入到文件,清空緩存
4、最后調(diào)用close來將流關閉:fileOutputStream.close();
//存儲數(shù)據(jù)
private void Save(String content) {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = openFileOutput(mFileName, MODE_PRIVATE);
fileOutputStream.write(content.getBytes());
fileOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 讀取數(shù)據(jù)
1、通過openFileInput(String name)來返回一個FileInputStream對象
2、byte[] buff = new byte[1024];這里是每1024個字節(jié)進行讀取一次
3、StringBuffer sb = new StringBuffer();創(chuàng)建StringBuffer來進行字符串的拼接
4、最后調(diào)用close來將流關閉:fileInputStream.close();
//讀取數(shù)據(jù)
private String read() {
FileInputStream fileInputStream = null;
try {
fileInputStream = openFileInput(mFileName);
byte[] buff = new byte[1024];
StringBuffer sb = new StringBuffer();
int len = 0;
while ((len = fileInputStream.read(buff)) > 0) {
sb.append(new String(buff, 0, len));
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}


外部存儲
- 前言
其實外部存儲方式和內(nèi)部存儲區(qū)別不是很大 只是存儲的位置發(fā)生了變化,內(nèi)部存儲用戶是看不到存儲的數(shù)據(jù),而外部存儲是存儲到手機外置內(nèi)存中 是可以查看到存儲的數(shù)據(jù)
- 存儲數(shù)據(jù)
前提需要進行寫入權限的申請
//外部存儲數(shù)據(jù)
private void fileSave(String content) {
FileOutputStream fileOutputStream = null;
try {
//第一步先創(chuàng)建文件夾
File dir = new File(Environment.getExternalStorageDirectory(), "dongbo");
if (!dir.exists()) {
//判斷文件夾是否存在 若不存在,則創(chuàng)建文件夾
dir.mkdirs();
}
//第二部創(chuàng)建文件
File file = new File(dir, mFileName);
if (!file.exists()) {
//判斷文件是否存在 若不存在,則創(chuàng)建文件
file.createNewFile();
}
fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(content.getBytes());
fileOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 讀取數(shù)據(jù)
//讀取外部數(shù)據(jù)
private String fileRead() {
FileInputStream fileInputStream = null;
try {
// fileInputStream = openFileInput(mFileName);
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "dongbo", mFileName);
fileInputStream = new FileInputStream(file);
byte[] buff = new byte[1024];
StringBuffer sb = new StringBuffer();
int len = 0;
while ((len = fileInputStream.read(buff)) > 0) {
sb.append(new String(buff, 0, len));
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
總結文件存儲
-
存儲路徑
1、mContext.getFilesDir().getAbsolutePath() 內(nèi)部存儲的File路徑
2、mContext.getCacheDir().getAbsolutePath() 內(nèi)部存儲的緩存路徑
3、mContext.getDir("project", MODE_PRIVATE) 創(chuàng)建新的文件路徑
4、Environment.getExternalStorageDirectory().getAbsolutePath() 外部存儲的路徑
5、mContext.deleteFile(mFileName);//刪除指定文件

-
mode操作模式 分兩種:
MODE_APPEND(文件創(chuàng)建模式,在openFileOuput方法中使用,如果文件存在,那么會在已存在的文件后面接著寫入數(shù)據(jù),而不是刪除已存在的數(shù)據(jù)。)大家可以看到這里的數(shù)據(jù)不是將之前的數(shù)據(jù)刪除之后重新創(chuàng)建,而是直接進行添加
MODE_PRIVATE(文件創(chuàng)建模式,默認的模式,用這個模式創(chuàng)建的文件只能被當前調(diào)用的應用程序訪問。(或者所有擁有相同UID的應用,這個UID其實就是每個進程的UID,也就是說同一進程訪問,這里涉及到多進程的知識,在此不詳細展開了))
說明:
自Api17以來,常量MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE已經(jīng)被棄用。
從Android N開始,使用這些常量會引發(fā)securityException。
這意味著,面向Android N 和更高版本的應用無法按名稱共享私有文件,嘗試共享file://URL將會導致引發(fā)FileUriExposedException。
如果應用需要與其他應用共享私有文件,則可以將FileProvider與FLAG_GRANT_READ_URI_PERMISSION配合使用。
- 涉及用戶
當用戶卸載應用之后,內(nèi)部存儲的文件也會隨之卸載而刪除,而外部存儲則不會刪除
- 存儲的范圍
存儲的數(shù)據(jù)可以是文本、照片、pdf文件,但是需要注意的是都需要把數(shù)據(jù)轉(zhuǎn)換成字節(jié)的形式來進行寫入,讀取的時候也是根據(jù)字節(jié)的形式來讀取,將數(shù)據(jù)轉(zhuǎn)換成指定類型的數(shù)據(jù)
若有不足,請大家指教
數(shù)據(jù)存儲相關文章
Android數(shù)據(jù)存儲之文件存儲
Android數(shù)據(jù)存儲之SharedPreferences
Android數(shù)據(jù)存儲之SQLite存儲