国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 數據存儲方式有哪幾種

2020-04-11 11:19:31
字體:
來源:轉載
供稿:網友

以下內容給大家介紹Android數據存儲提供了五種方式:

1、SharedPreferences

2、文件存儲

3、SQLite數據庫

4、ContentProvider

5、網絡存儲

  本文主要介紹如何使用文件來存儲數據。Android文件操作用到的是Java.IO中的FileOutputStream和FileInputStream類。

一、存儲文件

  首先實例化一個FileOutputStream。

FileOutputStream foStream = openFileOutput(fileName, MODE_PRIVATE);
// fileName: 要寫入文件的名稱
// MODE_PRIVATE: 為默認操作模式,代表該文件是私有數據,只能被應用本身訪問,在該模式下,寫入的內容會覆蓋原文件的內容
// MODE_APPEND: 模式會檢查文件是否存在,存在就往文件追加內容,否則就創建新文件.
// MODE_WORLD_READABLE: 表示當前文件可以被其他應用讀取,不推薦使用
// MODE_WORLD_WRITEABLE: 表示當前文件可以被其他應用寫入,不推薦使用

  然后調用foStream.write()即可完成寫入。

byte[] buffer = fileContent.getBytes();
foStream.write(buffer);
Toast.makeText(MainActivity.this, "寫入成功",Toast.LENGTH_SHORT).show();

  最后進行一些清理工作,刷新寫出流和關閉流。

foStream.flush();
foStream.close();

二、讀取文件

  同樣的,首先實例化一個FileInputStream。

FileInputStream fiStream = openFileInput(fileName)

  然后調用fiStream.read()即可

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();

三、完整代碼

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="文件內容" />   <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>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 朔州市| 济源市| 湘潭县| 三河市| 沽源县| 新化县| 鄂尔多斯市| 平安县| 余江县| 林州市| 澄迈县| 汝阳县| 淮阳县| 改则县| 江孜县| 德庆县| 玉环县| 昌都县| 莆田市| 岳阳县| 花莲县| 乌拉特后旗| 永兴县| 郴州市| 项城市| 宣汉县| 三明市| 体育| 乌兰县| 营山县| 五华县| 乌兰察布市| 报价| 全南县| 志丹县| 霍林郭勒市| 桓台县| 漳州市| 林甸县| 晋城| 安宁市|