Android 數(shù)據(jù)存儲方式有哪幾種
以下內(nèi)容給大家介紹Android數(shù)據(jù)存儲提供了五種方式:
1、SharedPreferences
2、文件存儲
3、SQLite數(shù)據(jù)庫
4、ContentProvider
5、網(wǎng)絡(luò)存儲
本文主要介紹如何使用文件來存儲數(shù)據(jù)。Android文件操作用到的是Java.IO中的FileOutputStream和FileInputStream類。
一、存儲文件
首先實(shí)例化一個FileOutputStream。
FileOutputStream foStream = openFileOutput(fileName, MODE_PRIVATE);
// fileName: 要寫入文件的名稱
// MODE_PRIVATE: 為默認(rèn)操作模式,代表該文件是私有數(shù)據(jù),只能被應(yīng)用本身訪問,在該模式下,寫入的內(nèi)容會覆蓋原文件的內(nèi)容
// MODE_APPEND: 模式會檢查文件是否存在,存在就往文件追加內(nèi)容,否則就創(chuàng)建新文件.
// MODE_WORLD_READABLE: 表示當(dāng)前文件可以被其他應(yīng)用讀取,不推薦使用
// MODE_WORLD_WRITEABLE: 表示當(dāng)前文件可以被其他應(yīng)用寫入,不推薦使用
然后調(diào)用foStream.write()即可完成寫入。
byte[] buffer = fileContent.getBytes();
foStream.write(buffer);
Toast.makeText(MainActivity.this, "寫入成功",Toast.LENGTH_SHORT).show();
最后進(jìn)行一些清理工作,刷新寫出流和關(guān)閉流。
foStream.flush();
foStream.close();
二、讀取文件
同樣的,首先實(shí)例化一個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, "寫入成功",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è)備掛載的所有存儲器
- android中使用SharedPreferences進(jìn)行數(shù)據(jù)存儲的操作方法
- Android調(diào)用相機(jī)并將照片存儲到sd卡上實(shí)現(xiàn)方法
- Android App將數(shù)據(jù)寫入內(nèi)部存儲和外部存儲的示例
- Android編程中的5種數(shù)據(jù)存儲方式
- Android7.0版本影響開發(fā)的改進(jìn)分析
- Android 7.0新特性詳解
- 詳解Android 7.0 Settings 加載選項
- Android 7.0中拍照和圖片裁剪適配的問題詳解
- Android 7.0調(diào)用相機(jī)崩潰詳解及解決辦法
- Android 7.0 Nougat不得不知的11項新功能
- Android 7.0開發(fā)獲取存儲設(shè)備信息的方法
相關(guān)文章
Android項目實(shí)戰(zhàn)之仿網(wǎng)易新聞的頁面(RecyclerView )
這篇文章主要介紹了Android項目實(shí)戰(zhàn)之仿網(wǎng)易新聞的頁面,ViewPager作為RecyclerView的Header,感興趣的小伙伴們可以參考一下2016-01-01
詳解Android StrictMode嚴(yán)格模式的使用方法
這篇文章主要介紹了Android StrictMode嚴(yán)格模式的使用方法,需要的朋友可以參考下2018-01-01
Android判斷NavigationBar是否顯示的方法(獲取屏幕真實(shí)的高度)
有些時候,我們需要知道當(dāng)前手機(jī)上是否顯示了NavigationBar,也就是屏幕底部的虛擬按鍵。這篇文章主要介紹了Android判斷NavigationBar是否顯示的方法(獲取屏幕真實(shí)的高度),需要的朋友可以參考下本文2017-01-01
Android中Property Animation屬性動畫編寫的實(shí)例教程
這篇文章主要介紹了Android中Property Animation屬性動畫編寫的實(shí)例教程,Property Animation對于動畫幀的操控十分強(qiáng)大,需要的朋友可以參考下2016-04-04
關(guān)于Android Fragment對回退棧的詳細(xì)理解
這篇文章主要介紹了Android Fragment的回退棧示例詳細(xì)介紹的相關(guān)資料,在Android中Fragment回退棧是由Activity管理的,每個Activity都有自己的回退棧,其中保存了已經(jīng)停止(處于后臺)的Fragment實(shí)例,需要的朋友可以參考下2016-12-12
解決Android studio Error:(30, 31) 錯誤: 程序包 不存在的問題
這篇文章主要介紹了解決Android studio Error:(30, 31) 錯誤: 程序包 不存在的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android字符串轉(zhuǎn)Ascii碼實(shí)例代碼
這篇文章主要介紹了Android字符串轉(zhuǎn)Ascii碼的方法,大家參考使用2013-11-11

