編輯:關於 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 等)的適用情境。
Android AIDL 與 Binder 框架淺析:從跨進程加減法看底層通信
1、概述 Binder 能給我們什麼?答案是:系統中任何程序都能訪問的全局服務。這幾乎是任何作業系統都必須具備的能力。先從宏觀結構看 Android 的 Binder 框
動手打造 Material Design 風格 Android 應用(一):主題與配色
本文原發表於個人部落格 isming.me。 Android 5.0 正式發表後,Android Developer 網站也補上了建立 Material Design
Hello,Android:從零開始寫出第一個安卓程式
專案緣起因為要參加某個資訊安全比賽,我選了「安卓 APK 行為分析與評估」這個課題,所以第一步得先搞懂安卓程式是怎麼撰寫和運行的。我們的第一個任務,就是寫出一個多人通訊的
Android 標籤控件封裝:可自動換行的 FlowLayout 標籤列表
版本:1.0 日期:2014.7.24 版權:© 2014 kince(轉載請註明出處) 很多應用需要提供一些標籤供使用者點選查詢,例如手機助手或購物軟體的分類標籤。開發