Android基礎(chǔ)教程數(shù)據(jù)存儲(chǔ)之文件存儲(chǔ)
Android基礎(chǔ)教程數(shù)據(jù)存儲(chǔ)之文件存儲(chǔ)
將數(shù)據(jù)存儲(chǔ)到文件中并讀取數(shù)據(jù)
1、新建FilePersistenceTest項(xiàng)目,并修改activity_main.xml中的代碼,如下:(只加入了EditText,用于輸入文本內(nèi)容,不管輸入什么按下back鍵就丟失,我們要做的是數(shù)據(jù)被回收之前,將它存儲(chǔ)在文件中)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
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>
2、修改MainActivity中的代碼,如下:(save()方法將一段文本內(nèi)容保存到文件中,load()方法從文件中讀取數(shù)據(jù),套用)
public class MainActivity extends AppCompatActivity {
private EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit=(EditText) findViewById(R.id.edit);
String inputText=load();
if(!TextUtils.isEmpty(inputText)){ //對(duì)字符串進(jìn)行非空判斷
edit.setText(inputText);
edit.setSelection(inputText.length());
Toast.makeText(this,"Restoring succeeded",Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onDestroy(){ //重寫(xiě)onDestroy()保證在活動(dòng)銷(xiāo)毀之前一定調(diào)用這個(gè)方法
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();
}
}
}
public String load(){
FileInputStream in=null;
BufferedReader 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();
}
}
運(yùn)行程序,效果如下:(輸入content后按back鍵返回,重新打開(kāi))

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- android?studio數(shù)據(jù)存儲(chǔ)建立SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)增刪查改
- Android 通過(guò)SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)管理
- Android四種數(shù)據(jù)存儲(chǔ)的應(yīng)用方式
- Android SharedPreferences實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)功能
- android使用SharedPreferences進(jìn)行數(shù)據(jù)存儲(chǔ)
- Android開(kāi)發(fā)教程之ContentProvider數(shù)據(jù)存儲(chǔ)
- 詳解Android的網(wǎng)絡(luò)數(shù)據(jù)存儲(chǔ)
- 5種Android數(shù)據(jù)存儲(chǔ)方式匯總
- 詳解Android數(shù)據(jù)存儲(chǔ)之SQLCipher數(shù)據(jù)庫(kù)加密
- Android 單例模式實(shí)現(xiàn)可復(fù)用數(shù)據(jù)存儲(chǔ)的詳細(xì)過(guò)程
相關(guān)文章
android學(xué)習(xí)筆記之View的滑動(dòng)
Android開(kāi)發(fā)中我們常常需要View滑動(dòng)實(shí)現(xiàn)一些絢麗的效果來(lái)優(yōu)化用戶體驗(yàn),下面這篇文章主要給大家介紹了關(guān)于android學(xué)習(xí)筆記之View滑動(dòng)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
Android采用GET方法進(jìn)行網(wǎng)絡(luò)傳值
這篇文章主要為大家詳細(xì)介紹了Android采用GET方法進(jìn)行網(wǎng)絡(luò)傳值的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android實(shí)現(xiàn)調(diào)用震動(dòng)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)調(diào)用震動(dòng)的方法,實(shí)例分析了Android中Vibrator類(lèi)的調(diào)用與使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android webview加載https鏈接錯(cuò)誤或無(wú)響應(yīng)的解決
這篇文章主要介紹了Android webview加載https鏈接錯(cuò)誤或無(wú)響應(yīng)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android仿探探卡片式滑動(dòng)效果實(shí)現(xiàn)
之前在玩探探看著效果圖還可以,然后又在網(wǎng)上看到了一相關(guān)的介紹,便想著自己動(dòng)手來(lái)實(shí)踐下,所以下面這篇文章主要介紹了關(guān)于Android實(shí)現(xiàn)仿探探卡片式左右滑動(dòng)效果的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-03-03
Flutter實(shí)現(xiàn)底部和頂部導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了Flutter實(shí)現(xiàn)底部和頂部導(dǎo)航欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07

