Android 數(shù)據(jù)存儲(chǔ)方式有哪幾種
以下內(nèi)容給大家介紹Android數(shù)據(jù)存儲(chǔ)提供了五種方式:
1、SharedPreferences
2、文件存儲(chǔ)
3、SQLite數(shù)據(jù)庫(kù)
4、ContentProvider
5、網(wǎng)絡(luò)存儲(chǔ)
本文主要介紹如何使用文件來(lái)存儲(chǔ)數(shù)據(jù)。Android文件操作用到的是Java.IO中的FileOutputStream和FileInputStream類(lèi)。
一、存儲(chǔ)文件
首先實(shí)例化一個(gè)FileOutputStream。
FileOutputStream foStream = openFileOutput(fileName, MODE_PRIVATE);
// fileName: 要寫(xiě)入文件的名稱
// MODE_PRIVATE: 為默認(rèn)操作模式,代表該文件是私有數(shù)據(jù),只能被應(yīng)用本身訪問(wèn),在該模式下,寫(xiě)入的內(nèi)容會(huì)覆蓋原文件的內(nèi)容
// MODE_APPEND: 模式會(huì)檢查文件是否存在,存在就往文件追加內(nèi)容,否則就創(chuàng)建新文件.
// MODE_WORLD_READABLE: 表示當(dāng)前文件可以被其他應(yīng)用讀取,不推薦使用
// MODE_WORLD_WRITEABLE: 表示當(dāng)前文件可以被其他應(yīng)用寫(xiě)入,不推薦使用
然后調(diào)用foStream.write()即可完成寫(xiě)入。
byte[] buffer = fileContent.getBytes();
foStream.write(buffer);
Toast.makeText(MainActivity.this, "寫(xiě)入成功",Toast.LENGTH_SHORT).show();
最后進(jìn)行一些清理工作,刷新寫(xiě)出流和關(guān)閉流。
foStream.flush();
foStream.close();
二、讀取文件
同樣的,首先實(shí)例化一個(gè)FileInputStream。
FileInputStream fiStream = openFileInput(fileName)
然后調(diào)用fiStream.read()即可
int len = fiStream.available();
byte[] buffer = new byte[len];
fiStream.read(buffer)
最后,將文本顯示并關(guān)閉讀文件流
etContent.setText(new String(buffer));
Toast.makeText(MainActivity.this, "讀取成功",Toast.LENGTH_SHORT).show();
fiStream.close();
三、完整代碼
import android.support.v.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.io.FileInputStream; import java.io.FileOutputStream; public class MainActivity extends AppCompatActivity { private EditText etName; private EditText etContent; private Button btnWrite; private Button btnRead; private String fileName = ""; private String fileContent = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etName = (EditText)findViewById(R.id.etName); etContent = (EditText)findViewById(R.id.etContent); btnWrite = (Button)findViewById(R.id.btnWrite); btnRead = (Button)findViewById(R.id.btnRead); btnWrite.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { fileName = etName.getText().toString(); fileContent = etContent.getText().toString(); try { FileOutputStream foStream = openFileOutput(fileName, MODE_PRIVATE); byte[] buffer = fileContent.getBytes(); foStream.write(buffer); Toast.makeText(MainActivity.this, "寫(xiě)入成功",Toast.LENGTH_SHORT).show(); foStream.flush(); foStream.close(); }catch(Exception e){ e.printStackTrace(); } } }); btnRead.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { fileName = etName.getText().toString(); try{ FileInputStream fiStream = openFileInput(fileName); int len = fiStream.available(); byte[] buffer = new byte[len]; fiStream.read(buffer); etContent.setText(new String(buffer)); Toast.makeText(MainActivity.this, "讀取成功",Toast.LENGTH_SHORT).show(); fiStream.close(); }catch(Exception e){ e.printStackTrace(); } } }); } } <RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/etName" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:text="文件名" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/etContent" android:layout_below="@+id/etName" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:text="文件內(nèi)容" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="保存" android:id="@+id/btnWrite" android:layout_alignTop="@+id/btnRead" android:layout_toLeftOf="@+id/btnRead" android:layout_toStartOf="@+id/btnRead" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="讀取" android:id="@+id/btnRead" android:layout_below="@+id/etContent" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout>
- 如何獲取Android設(shè)備掛載的所有存儲(chǔ)器
- android中使用SharedPreferences進(jìn)行數(shù)據(jù)存儲(chǔ)的操作方法
- Android調(diào)用相機(jī)并將照片存儲(chǔ)到sd卡上實(shí)現(xiàn)方法
- Android App將數(shù)據(jù)寫(xiě)入內(nèi)部存儲(chǔ)和外部存儲(chǔ)的示例
- Android編程中的5種數(shù)據(jù)存儲(chǔ)方式
- Android7.0版本影響開(kāi)發(fā)的改進(jìn)分析
- Android 7.0新特性詳解
- 詳解Android 7.0 Settings 加載選項(xiàng)
- Android 7.0中拍照和圖片裁剪適配的問(wèn)題詳解
- Android 7.0調(diào)用相機(jī)崩潰詳解及解決辦法
- Android 7.0 Nougat不得不知的11項(xiàng)新功能
- Android 7.0開(kāi)發(fā)獲取存儲(chǔ)設(shè)備信息的方法
相關(guān)文章
android webview 中l(wèi)ocalStorage無(wú)效的解決方法
這篇文章主要介紹了android webview 中l(wèi)ocalStorage無(wú)效的解決方法,本文直接給出解決方法實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-06-06Android組件WebView編寫(xiě)有道詞典小案例分享
這篇文章主要為大家分享了Android組件WebView編寫(xiě)有道詞典小案例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05Flutter滾動(dòng)組件之SingleChildScrollView使用詳解
這篇文章主要為大家詳細(xì)介紹了Flutter滾動(dòng)組件之SingleChildScrollView使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Android利用RenderScript實(shí)現(xiàn)毛玻璃模糊效果示例
毛玻璃效果(亦稱磨砂效果),近兩年在移動(dòng)端的UI設(shè)計(jì)上越來(lái)越流行,下面這篇文章主要介紹了Android利用RenderScript實(shí)現(xiàn)毛玻璃模糊效果的相關(guān)資料,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考學(xué)習(xí),下面來(lái)一起看看吧。2017-03-03Android添加(創(chuàng)建)、刪除及判斷是否存在桌面快捷方式的方法
這篇文章主要介紹了Android添加(創(chuàng)建)、刪除及判斷是否存在桌面快捷方式的方法,涉及Android針對(duì)桌面快捷方式的相關(guān)操作技巧,需要的朋友可以參考下2015-05-05Android應(yīng)用退出登錄的實(shí)現(xiàn)方法
每一個(gè)app都會(huì)有一個(gè)”退出登陸”的功能,當(dāng)點(diǎn)擊退出之后需要將所有的Activity都finish掉,開(kāi)始是想將棧中的所有Activity清除掉,但是沒(méi)有找到方法,后來(lái)用廣播實(shí)現(xiàn)了。下面小編給大家分享android應(yīng)用退出登錄的實(shí)現(xiàn)方法,需要的朋友參考下2017-04-04Android中WebChromeClient和WebViewClient的區(qū)別淺析
這篇文章主要介紹了Android中WebChromeClient和WebViewClient的區(qū)別淺析,需要的朋友可以參考下2015-04-04android?Service基礎(chǔ)(啟動(dòng)服務(wù)與綁定服務(wù))
大家好,本篇文章主要講的是android?Service基礎(chǔ)(啟動(dòng)服務(wù)與綁定服務(wù)),感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Android使用OkHttp進(jìn)行網(wǎng)絡(luò)同步異步操作
這篇文章主要為大家詳細(xì)介紹了Android使用OkHttp進(jìn)行網(wǎng)絡(luò)同步異步操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07