實(shí)例詳解Android文件存儲(chǔ)數(shù)據(jù)方式
總體的來講,數(shù)據(jù)存儲(chǔ)方式有三種:一個(gè)是文件,一個(gè)是數(shù)據(jù)庫,另一個(gè)則是網(wǎng)絡(luò)。下面通過本文給大家介紹Android文件存儲(chǔ)數(shù)據(jù)方式。
1.文件存儲(chǔ)數(shù)據(jù)使用了Java中的IO操作來進(jìn)行文件的保存和讀取,只不過Android在Context類中封裝好了輸入流和輸出流的獲取方法。
創(chuàng)建的存儲(chǔ)文件保存在/data/data/<package name>/files文件夾下。

2.操作。
保存文件內(nèi)容:通過Context.openFileOutput獲取輸出流,參數(shù)分別為文件名和存儲(chǔ)模式。
讀取文件內(nèi)容:通過Context.openFileInput獲取輸入流,參數(shù)為文件名。
刪除文件:Context.deleteFile刪除指定的文件,參數(shù)為將要?jiǎng)h除的文件的名稱。
獲取文件名列表:通過Context.fileList獲取files目錄下的所有文件名數(shù)組。
*獲取文件路徑的方法:
絕對路徑:/data/data/<package name>/files/filename
Context:Context.getFilesDir()可以獲取到"/data/data/<package name>/files"
3.四種文件保存的模式。
Context.MODE_PRIVATE 為默認(rèn)操作模式,代表該文件是私有數(shù)據(jù),只能被應(yīng)用本身訪問,在該模式下寫入的內(nèi)容會(huì)覆蓋原文件的內(nèi)容。
Context.MODE_APPEND 檢查文件是否存在,存在就往文件追加內(nèi)容,否則就創(chuàng)建新文件。
MODE_WORLD_READABLE 表示當(dāng)前文件可以被其他應(yīng)用讀取。
MODE_WORLD_WRITEABLE 表示當(dāng)前文件可以被其他應(yīng)用寫入。
在使用模式時(shí),可以用"+"來選擇多種模式,比如openFileOutput(FILENAME, Context.MODE_PRIVATE + MODE_WORLD_READABLE);
下面通過程序來演示下文件存儲(chǔ)的使用。完整代碼下載:android_files.rar
/**
* MainActivity
*
* @author zuolongsnail
*
*/
public class MainActivity extends Activity {
private EditText writeET;
private Button writeBtn;
private TextView contentView;
public static final String FILENAME = "setting.set";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
writeET = (EditText) findViewById(R.id.write_et);
writeBtn = (Button) findViewById(R.id.write_btn);
contentView = (TextView) findViewById(R.id.contentview);
writeBtn.setOnClickListener(new OperateOnClickListener());
}
class OperateOnClickListener implements OnClickListener {
@Override
public void onClick(View v) {
writeFiles(writeET.getText().toString());
contentView.setText(readFiles());
System.out.println(getFilesDir());
}
}
// 保存文件內(nèi)容
private void writeFiles(String content) {
try {
// 打開文件獲取輸出流,文件不存在則自動(dòng)創(chuàng)建
FileOutputStream fos = openFileOutput(FILENAME,
Context.MODE_PRIVATE);
fos.write(content.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 讀取文件內(nèi)容
private String readFiles() {
String content = null;
try {
FileInputStream fis = openFileInput(FILENAME);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
content = baos.toString();
fis.close();
baos.close();
} catch (Exception e) {
e.printStackTrace();
}
return content;
}
}
程序截圖:

提供一個(gè)文件存儲(chǔ)數(shù)據(jù)的工具類:
/**
* 文件存儲(chǔ)數(shù)據(jù)方式工具類
*
* @author zuolongsnail
*/
public class FilesUtil {
/**
* 保存文件內(nèi)容
*
* @param c
* @param fileName
* 文件名稱
* @param content
* 內(nèi)容
*/
private void writeFiles(Context c, String fileName, String content, int mode)
throws Exception {
// 打開文件獲取輸出流,文件不存在則自動(dòng)創(chuàng)建
FileOutputStream fos = c.openFileOutput(fileName, mode);
fos.write(content.getBytes());
fos.close();
}
/**
* 讀取文件內(nèi)容
*
* @param c
* @param fileName
* 文件名稱
* @return 返回文件內(nèi)容
*/
private String readFiles(Context c, String fileName) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis = c.openFileInput(fileName);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
String content = baos.toString();
fis.close();
baos.close();
return content;
}
}
以上通過實(shí)例詳解Android文件存儲(chǔ)數(shù)據(jù)方式,希望對大家今后的工作學(xué)習(xí)有所幫助。
- 詳解Android數(shù)據(jù)存儲(chǔ)之Android 6.0運(yùn)行時(shí)權(quán)限下文件存儲(chǔ)的思考
- Android實(shí)現(xiàn)文件存儲(chǔ)并讀取的示例代碼
- android數(shù)據(jù)存儲(chǔ)之文件存儲(chǔ)方法
- android開發(fā)基礎(chǔ)教程—文件存儲(chǔ)功能實(shí)現(xiàn)
- Android圖片添加水印圖片并把圖片保存到文件存儲(chǔ)的實(shí)現(xiàn)代碼
- 詳解Android開發(fā)數(shù)據(jù)持久化之文件存儲(chǔ)(附源碼)
- Android學(xué)習(xí)之文件存儲(chǔ)讀取
- 詳解Android文件存儲(chǔ)
- Android編程之SharedPreferences文件存儲(chǔ)操作實(shí)例分析
- Android開發(fā)文件存儲(chǔ)實(shí)例
相關(guān)文章
Android自定義View實(shí)現(xiàn)簡單文字描邊功能
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)簡單文字描邊功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Flutter app頁面路由以及路由攔截的實(shí)現(xiàn)
本篇介紹了介紹了Flutter如何使用路由來實(shí)現(xiàn)頁面的跳轉(zhuǎn),從而簡化頁面之間的耦合,并可以實(shí)現(xiàn)路由攔截。2021-06-06
Android手勢密碼view學(xué)習(xí)筆記(一)
這篇文章主要為大家詳細(xì)介紹了Android手勢密碼view的學(xué)習(xí)筆記,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android如何使用ViewPager2實(shí)現(xiàn)頁面滑動(dòng)切換效果
這篇文章主要給大家介紹了關(guān)于Android如何使用ViewPager2實(shí)現(xiàn)頁面滑動(dòng)切換效果的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02
Android SharedPreferences實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)功能
這篇文章主要為大家詳細(xì)介紹了Android SharedPreferences實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Android下Button實(shí)現(xiàn)圖文混排效果
這篇文章主要為大家詳細(xì)介紹了Android下Button實(shí)現(xiàn)圖文混排效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android通過AlarmManager類實(shí)現(xiàn)簡單鬧鐘功能
這篇文章主要為大家詳細(xì)介紹了Android通過AlarmManager類實(shí)現(xiàn)簡單鬧鐘功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
Android開發(fā)之ListView列表刷新和加載更多實(shí)現(xiàn)方法
這篇文章主要介紹了Android開發(fā)之ListView列表刷新和加載更多實(shí)現(xiàn)方法,實(shí)例分析了ListView列表操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06

