Android文件存儲方案詳解:內部存儲、外部存儲、SQLite資料庫

在Android開發中,文件讀寫是一項基本操作,為了支持文件的讀寫操作,Android系統提供了多種文件存儲方式,如內部存儲、外部存儲和SQLite資料庫等。本文將詳細介紹這些文件存儲方式的特點、使用場景及相關示例。

一、內部存儲

內部存儲是指將文件存儲在應用程序的私有目錄中,私有目錄只能被本應用程序訪問,其他應用程序無法訪問。內部存儲的優點是數據安全性高,不容易被其他應用程序篡改。以下是內部存儲的使用方法:

1、創建文件

File file = new File(getFilesDir(), "example.txt");
try {
    FileWriter writer = new FileWriter(file);
    writer.write("Hello world!");
    writer.flush();
    writer.close();
} catch (IOException e) {
    e.printStackTrace();
}

2、讀取文件

File file = new File(getFilesDir(), "example.txt");
try {
    FileReader reader = new FileReader(file);
    char[] buf = new char[1024];
    int len;
    StringBuilder sb = new StringBuilder();
    while ((len = reader.read(buf)) != -1) {
        sb.append(new String(buf, 0, len));
    }
    reader.close();
    String content = sb.toString();
} catch (IOException e) {
    e.printStackTrace();
}

3、刪除文件

File file = new File(getFilesDir(), "example.txt");
if (file.exists()) {
    file.delete();
}

二、外部存儲

外部存儲是指將文件存儲在SD卡上,可以被多個應用程序共享,但是需要注意SD卡的插拔情況,如果沒有進行判斷,在SD卡未掛載的情況下使用會導致應用程序崩潰。以下是外部存儲的使用方法:

1、創建文件

File file = new File(getExternalFilesDir(null), "example.txt");
try {
    FileWriter writer = new FileWriter(file);
    writer.write("Hello world!");
    writer.flush();
    writer.close();
} catch (IOException e) {
    e.printStackTrace();
}

2、讀取文件

File file = new File(getExternalFilesDir(null), "example.txt");
try {
    FileReader reader = new FileReader(file);
    char[] buf = new char[1024];
    int len;
    StringBuilder sb = new StringBuilder();
    while ((len = reader.read(buf)) != -1) {
        sb.append(new String(buf, 0, len));
    }
    reader.close();
    String content = sb.toString();
} catch (IOException e) {
    e.printStackTrace();
}

3、刪除文件

File file = new File(getExternalFilesDir(null), "example.txt");
if (file.exists()) {
    file.delete();
}

三、SQLite資料庫

SQLite資料庫是一種輕量級的關係型資料庫,適用於存儲結構化數據,如應用程序的配置信息、用戶數據等。以下是SQLite資料庫的使用方法:

1、創建資料庫

public class ExampleDatabaseHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "example.db";
    private static final int DB_VERSION = 1;

    public ExampleDatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE IF NOT EXISTS example(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}

2、插入數據

ExampleDatabaseHelper databaseHelper = new ExampleDatabaseHelper(this);
SQLiteDatabase db = databaseHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("name", "Tom");
cv.put("age", 18);
db.insert("example", null, cv);
db.close();

3、查詢數據

ExampleDatabaseHelper databaseHelper = new ExampleDatabaseHelper(this);
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Cursor cursor = db.query("example", null, null, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
    do {
        int id = cursor.getInt(cursor.getColumnIndex("id"));
        String name = cursor.getString(cursor.getColumnIndex("name"));
        int age = cursor.getInt(cursor.getColumnIndex("age"));
    } while (cursor.moveToNext());
    cursor.close();
}
db.close();

4、刪除數據

ExampleDatabaseHelper databaseHelper = new ExampleDatabaseHelper(this);
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.delete("example", "id=?", new String[]{"1"});
db.close();

通過以上示例代碼,我們可以了解到Android中不同的文件存儲方式的使用方法和適用情況。在實際開發中,我們可以根據需求選擇合適的文件存儲方式,實現數據的讀寫操作。

原創文章,作者:YIFP,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/137828.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
YIFP的頭像YIFP
上一篇 2024-10-04 00:18
下一篇 2024-10-04 00:18

相關推薦

發表回復

登錄後才能評論