一、持久化技術
- 我們平時所使用的APP產生的資料,在記憶體中都是瞬時的,會隨著斷電、關機等丟失資料,因此android系統采用了持久化技術,用于存盤這些“瞬時”資料
- 持久化技術包括:檔案存盤、SharedPreference存盤以及資料庫存盤,還有更復雜的SD卡記憶體儲,
二、檔案存盤
- 最基本存盤方式,不對存盤內容進行格式化處理,適用于存盤簡單文本或者二進制資料,若存盤一些復雜資料,那么需要定義格式規范,方便后續決議出來,
Context
類提供了openFileOutput
方法用于將資料存盤到檔案中,- 該方法接收兩個引數,第一個為檔案名(不可以包含路徑,因為資料已經默認存盤到
/data/data<packagename>/files/
目錄下面了,第二個引數時檔案的操作模式,包括兩種MODE_PRIVATE
和MODE_APPEND
,前一個是默認,縮寫內容會覆寫,后一個表示追加內容,不存在就創建檔案, - openFileOutput()方法回傳的是一個
FileOutputStream
物件,得到該物件之后,可以使用流的方式寫入檔案,以下是一段簡單的演示:
public void save(){
String data = https://www.cnblogs.com/ruigege0000/p/"Data to save";
FileOutputStream out = null;
BufferedWriter writer = null;
try{
out = openFileOutput("data",Context.MODE_PRIVATE);
writer = new BufferedWrirter(new OutputStreamWriter(out));
writer.write(data);
} catch (IOException e){
e.printStackTrace();
} finally {
try{
if(writer != null){
writer.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
- 如上就是寫一個存盤檔案的Java流的一系列操作,如果不明白其中的含義,可以參見Java連載的流的那幾期,
1.建立一個FilePersistenceTest專案用于演示
- 先修改
anctivity_main.xml
檔案代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type something here" />
</LinearLayout>
- 運行顯示:
- 可以看到APP有一個地方可以寫入文本,但是這都是臨時檔案,一退出就沒了,因此我們需要改一下這個主xml的活動邏輯
MainActivity.java
檔案
package com.example.filepersistencetest;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText)findViewById(R.id.edit);
}
@Override
protected void onDestroy() {
super.onDestroy();
String inputText = edit.getText().toString();
save(inputText);
}
public void save(String inputText) {
FileOutputStream out = null;
BufferedWriter writer = null;
try{
out = openFileOutput("data",Context.MODE_PRIVATE);
writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(inputText);
} catch (IOException e){
e.printStackTrace();
} finally {
try{
if(writer != null){
writer.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
-
決議:我們在主活動中獲取了EditText標簽的文本內容,保存在了edit變數中,然后重寫了onDestroy()方法,保證了在銷毀這個活動的時候,能夠保證內容已經保存在APP中,save方法基本和我們之前舉例的一致,
-
那么如果確認我們已經保存了資料呢?
-
打開DDMS的File Explorer來查看一下,按圖示打開檔案
com.example.persistencetest/files/
目錄下面內容,并且到處,使用notepad++打開看一看,確實是我們之前輸入的
2.如何從檔案中讀取資料
- 讀取檔案的函式
openFileInput()
方法,用于從檔案中讀取資料,引數只有一個即使檔案名,當然也不需要路徑,因為android已經提前定義好了/data/data/<packagename>/files/
目錄下面,并回傳一個FileInputStream
物件,演示一波
public String load(){
FileInputStream in = null;
BufferReader reader = null;
StringBuilder content = new StringBuilder();
try {
in = openFileInput("data");
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while((line=reader.readLine())!=null){
content.append(line);
}
}catch(IOException e){
e.printStackTrace();
}finally{
if(reader != null){
try{
reader.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
return content.toString();
}
- 上面的代碼顯示出一行一行的讀取出檔案內容,
三、原始碼:
- FilePersistenceTest
- 地址:https://github.com/ruigege66/Android/tree/master/FilePersistenceTest
- CSDN:https://blog.csdn.net/weixin_44630050
- 博客園:https://www.cnblogs.com/ruigege0000/
- 歡迎關注微信公眾號:傅里葉變換,個人賬號,僅用于技術交流
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/1223.html
標籤:Android