本文執行個體講述了Android使用檔案進行資料存放區的方法。分享給大家供大家參考。具體如下:
很多時候我們開發的軟體需要對處理後的資料進行儲存,以供再次訪問。Android為資料存放區提供了如下幾種方式:
檔案
SharedPreferences(參數)
SQLite資料庫
內容提供者(Content provider)
網路
首先給大家介紹使用檔案如何對資料進行儲存
Activity提供了openFileOutput()方法可以用於把資料輸出到檔案中,具體的實現過程與在J2SE環境中儲存資料到檔案中是一樣的
public class FileActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { FileOutputStream outStream = this.openFileOutput("ljq.txt", Context.MODE_PRIVATE); outStream.write("我叫林計欽".getBytes()); outStream.close(); }}
openFileOutput(fileName, mode)方法詳解:
第一參數:
用於指定檔案名稱,不能包含路徑分隔字元“/”。如果檔案不存在,Android 會自動建立它。建立的檔案儲存在/data/data/<packagename>/files目錄,如:/data/data/com.ljq.activity/files/itcast.txt ,通過點擊MyEclipse菜單“Window”-“Show View”-“Other”,
在交談視窗中展開android檔案夾,選擇下面的File Explorer視圖,然後在File Explorer視圖中展開/data/data/<package name>/files目錄就可以看到該檔案
第二參數:
用於指定操作模式,有四種模式
Context.MODE_PRIVATE = 0
預設操作模式,代表該檔案是私人資料,只能被應用本身訪問,在該模式下,寫入的內容會覆蓋原檔案的內容,如果想把新寫入的內容追加到原檔案中。
可以使用Context.MODE_APPEND。
Context.MODE_APPEND = 32768
檢查檔案是否存在,存在就往檔案追加內容,否則就建立新檔案。
Context.MODE_WORLD_READABLE = 1
表示當前檔案可以被其他應用讀取
Context.MODE_WORLD_WRITEABLE = 2
表示當前檔案可以被其他應用寫入
注意:
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用來控制其他應用是否有許可權讀寫該檔案。
如果希望檔案被其他應用讀和寫,可以傳入: openFileOutput("ljq.txt", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
android有一套自己的安全模型,當應用程式(.apk)在安裝時系統就會分配給他一個userid,當該應用要去訪問其他資源比如檔案的時候,就需要userid匹配。預設情況下,任何應用建立的檔案,sharedpreferences,資料庫都應該是私人的(位於/data/data/<package name>/files),其他程式無法訪問。除非在建立時指定了Context.MODE_WORLD_READABLE或者Context.MODE_WORLD_WRITEABLE ,只有這樣其他程式才能正確訪問。
如果要開啟存放在/data/data/<package name>/files目錄應用私人的檔案,可以使用Activity提供openFileInput()方法。
FileInputStream inStream = this.getContext().openFileInput("ljq.txt");
或者直接使用檔案的絕對路徑:
File file = new File("/data/data/com.ljq.action/files/ljq.txt");
FileInputStream inStream = new FileInputStream(file);
注意:上面檔案路徑中的“com.ljq.action”為應用所在包,當你在編寫代碼時應替換為你自己應用使用的包。
對於私人檔案只能被建立該檔案的應用訪問,如果希望檔案能被其他應用讀和寫,可以在建立檔案時,
指定Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE許可權。
Activity還提供了getCacheDir()和getFilesDir()方法:
getCacheDir()方法用於擷取/data/data/<package name>/cache目錄
getFilesDir()方法用於擷取/data/data/<package name>/files目錄
案例
FileService類:檔案存取操作類
package com.ljq.service;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.io.OutputStream;public class FileService { /** * 儲存資料 * * @param outputStream * @param content * @throws Exception */ public static void save(OutputStream outputStream, String content) throws Exception { outputStream.write(content.getBytes()); outputStream.close(); } /** * 讀取資料 * * @param inputStream * @return * @throws Exception */ public static String read(InputStream inputStream) throws Exception { // 往記憶體寫資料 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // 緩衝區 byte[] buffer = new byte[1024]; int len = -1; while ((len = inputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, len); } // 儲存資料 byte[] data = byteArrayOutputStream.toByteArray(); byteArrayOutputStream.close(); inputStream.close(); return new String(data); }}
FileServiceTest測試類別:
package com.ljq.service;import java.io.InputStream;import java.io.OutputStream;import android.content.Context;import android.test.AndroidTestCase;import android.util.Log;/** * android測試 * * @author jiqinlin * */public class FileServiceTest extends AndroidTestCase { private final String TAG = "FileServiceTest"; public void testSave() throws Exception{ OutputStream outputStream = this.getContext().openFileOutput("ljq.txt", Context.MODE_PRIVATE); FileService.save(outputStream, "abc"); } public void testRead() throws Exception{ InputStream inputStream= this.getContext().openFileInput("ljq.txt"); String content = FileService.read(inputStream); Log.i(TAG, content); }}
資訊清單檔:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ljq.activity" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <uses-library android:name="android.test.runner" /> <activity android:name=".FileActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="7" /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.ljq.activity" android:label="Tests for My App" /></manifest>
希望本文所述對大家的Android程式設計有所協助。