Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android五大資料儲存方式總覽:以檔案儲存為例實作讀寫

編輯:關於 Android 程式設計

資料儲存是App開發中不可或缺的環節。Android平台提供了五種主要的資料儲存方式:

1、SharedPreferences
2、檔案儲存
3、SQLite資料庫
4、ContentProvider
5、網路儲存

本文將聚焦於「檔案儲存」,示範如何使用 FileOutputStream 與 FileInputStream 這兩個 Java.IO 的類別來完成資料的寫入與讀取。

一、儲存檔案

首先建立一個 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="https://schemas.android.com/apk/res/android" xmlns:tools="https://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>

透過這份範例,你可以清楚掌握Android檔案讀寫的完整流程,並理解不同檔案模式(MODE_PRIVATE、MODE_APPEND 等)的適用情境。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved